欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

键值数据库了解mongoDB 博客分类: Database MongoDBJ#PythonjsonJavaScript 

程序员文章站 2024-03-15 13:36:23
...
键值数据库了解mongoDB

首页
http://www.mongodb.org/display/DOCS/Home

mongo stand for a kind of fruit.haha.but the explain of the website is humongous.
humongous [hju:'mʌŋɡəs]  adj. 巨大无比的,极大的
mongo ['mɔŋɡəu] n 芒果

下载地址
http://www.mongodb.org/display/DOCS/Downloads

我下载了windows版本的来试试。得到文件:mongodb-win32-i386-1.2.3.zip

文档地址:
http://www.mongodb.org/display/DOCS/Tutorial

Getting the Database
start the mongod process:
C:\mongodb-win32-i386-1.2.3\bin\mongod.exe

报错如下:
Thu Feb 25 17:21:31 Assertion: dbpath (/data/db/) does not exist
Thu Feb 25 17:21:31   exception in initAndListen std::exception: dbpath (/data/db/) does not exist, terminating

新建文件夹d:\database\mongo\db\,然后运行:
mongod.exe -dbpath d:\database\mongo\db\

启动成功!

Getting A Database Connection
Start the MongoDB JavaScript shell with:
C:\mongodb-win32-i386-1.2.3\bin>mongo.exe
MongoDB shell version: 1.2.3
url: test
connecting to: test
type "exit" to exit
type "help" for help
>
选择数据库
use mydb

Inserting Data into A Collection
> j = {name:"mongo"};
{ "name" : "mongo" }
> t = {x : 3};
{ "x" : 3 }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }
{ "_id" : ObjectId("4b87655c9a14000000003f6e"), "x" : 3 }

放入更多的元素
> for(var i = 1;i<10;i++) db.things.save({x:4,j:i})
> db.things.find();
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }
{ "_id" : ObjectId("4b87655c9a14000000003f6e"), "x" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4b8765d09a14000000003f70"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4b8765d09a14000000003f71"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f72"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4b8765d09a14000000003f73"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4b8765d09a14000000003f74"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4b8765d09a14000000003f75"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4b8765d09a14000000003f76"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4b8765d09a14000000003f77"), "x" : 4, "j" : 9 }

Accessing Data From a Query
> var cursor = db.things.find();
> while (cursor.hasNext()){print(tojson(cursor.next()));}
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }
{ "_id" : ObjectId("4b87655c9a14000000003f6e"), "x" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4b8765d09a14000000003f70"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4b8765d09a14000000003f71"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f72"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4b8765d09a14000000003f73"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4b8765d09a14000000003f74"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4b8765d09a14000000003f75"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4b8765d09a14000000003f76"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4b8765d09a14000000003f77"), "x" : 4, "j" : 9 }

The above example shows cursor-style iteration. The hasNext() function tells if there are any more documents to return, and the next() function returns the next document. We also used the built-in tojson() method to render the document in a pretty JSON-style format.

Repeating the example above, but using forEach() directly on the cursor rather than the while loop:
> db.things.find().forEach(function(x){ print(tojson(x));})
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }
{ "_id" : ObjectId("4b87655c9a14000000003f6e"), "x" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4b8765d09a14000000003f70"), "x" : 4, "j" : 2 }
...

In the mongo shell, you can also treat cursors like an array :
> var cursor = db.things.find();
> print(tojson(cursor[4]));
{ "_id" : ObjectId("4b8765d09a14000000003f71"), "x" : 4, "j" : 3 }

Specifying What the Query Returns
SELECT * FROM things WHERE name="mongo"
> db.things.find({name:"mongo"}).forEach(function(x){print(tojson(x));});
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }

SELECT * FROM things WHERE x=4
> db.things.find({x:4}).forEach(function(x){print(tojson(x));});
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4b8765d09a14000000003f70"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4b8765d09a14000000003f71"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f72"), "x" : 4, "j" : 4 }
...
A query document of the form { a:A, b:B, ... } means "where a==A and b==B and ...".

MongoDB also lets you return "partial documents" - documents that have only a subset of the elements of the document stored in the database.
SELECT j FROM things WHERE x=4
> db.things.find({x:4},{j:true}).forEach(function(x){print(tojson(x));});
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "j" : 1 }
{ "_id" : ObjectId("4b8765d09a14000000003f70"), "j" : 2 }
{ "_id" : ObjectId("4b8765d09a14000000003f71"), "j" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f72"), "j" : 4 }
...

findOne()
> var mongo = db.things.findOne({name:"mongo"});
> print(tojson(mongo))
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }

> var mongo = db.things.findOne({x : 4});
> print(tojson(mongo))
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "x" : 4, "j" : 1 }

Limiting the Result Set via limit()
> db.things.find().limit(3);
{ "_id" : ObjectId("4b8765509a14000000003f6d"), "name" : "mongo" }
{ "_id" : ObjectId("4b87655c9a14000000003f6e"), "x" : 3 }
{ "_id" : ObjectId("4b8765d09a14000000003f6f"), "x" : 4, "j" : 1 }

进一步学习文档
http://www.mongodb.org/display/DOCS/Manual

各种驱动下载地址
http://www.mongodb.org/display/DOCS/Drivers
我主要了解JAVA和python两种语言如何去操作这个数据库。嘿嘿。
java驱动地址
http://www.mongodb.org/display/DOCS/Java+Language+Center
python驱动地址
http://api.mongodb.org/python/1.4%2B/index.html

最先看看java的吧,下载得到文件:mongo-1.2.jar,引入到classpath,参考文档如下:
http://www.mongodb.org/display/DOCS/Java+Tutorial

参考官方文档,写了个简单的示例程序,看到别人的BLOG立面,还可以支持implements DBObject,我没有测试成功。不知道是不是版本问题。
package com.easymessage;

import java.net.UnknownHostException;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MainTest {

public static void main(String[] args) {
// connect to the database
Mongo m = null;
try {
m = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}

DB db = m.getDB("mydb");

// authenticate
boolean auth = db.authenticate("username", "pwd".toCharArray());
System.out.println(auth);

// show all the collections
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}

// get a collection
DBCollection coll = db.getCollection("test");

// insert some data
BasicDBObject user = new BasicDBObject();
user.put("name", "sillycat");
user.put("age", 28);
BasicDBObject address = new BasicDBObject();
address.put("place", "Chengdu");
address.put("code", 641000);
user.put("address", address);
coll.insert(user);

// find one data
DBObject myuser = coll.findOne();
System.out.println(myuser);

// insert data
for (int i = 0; i < 100; i++) {
coll.insert(new BasicDBObject().append("i", i));
}
// show the datacount
System.out.println(coll.getCount());

// use DBCursor
DBCursor cur = coll.find();
while (cur.hasNext()) {
cur.next();
// System.out.println(cur.next());
}

// select * from test where i = 71
BasicDBObject query = new BasicDBObject();
query.put("i", 71);
cur = coll.find(query);
while (cur.hasNext()) {
DBObject ob = cur.next();
System.out.println("kill========" + ob);
// remove one data
coll.remove(ob);
}

query = new BasicDBObject();
query.put("i", 71);
cur = coll.find(query);
System.out.println("left==========" + cur.count());

// select * from test where i > 99
query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 98));
cur = coll.find(query);
while (cur.hasNext()) {
System.out.println(cur.next());
}

// select * from test where 51<i<52
query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 51).append("$lte", 52));
cur = coll.find(query);
while (cur.hasNext()) {
System.out.println(cur.next());
}

// get a name list of the database
for (String s : m.getDatabaseNames()) {
System.out.println(s);
}

// get a database
DB temp_db = m.getDB("mydb");
System.out.println(temp_db.getName());

// drop a database by name
m.dropDatabase("mydb");

}

}