日期:2014-05-16  浏览次数:20552 次

键值数据库了解mongoDB
键值数据库了解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("4b8765