MongoDB试用记录

MongoDB试用记录

MongoDB是一种著名的NoSQL数据库。

操作系统:RHEL5.4

MongoDB版本:2.0.0

 

下载MonoDB并解压

[root@sunrise16 setup]# wget "http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.0.tgz"
--2011-10-20 12:56:36--  http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.0.tgz
正在解析主机 fastdl.mongodb.org... 216.137.45.220, 216.137.45.22, 216.137.45.29, ...
Connecting to fastdl.mongodb.org|216.137.45.220|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:37949452 (36M) [application/x-tar]
Saving to: `mongodb-linux-i686-2.0.0.tgz'

100%[==========================================================================================>] 37,949,452  44.6K/s   in 8m 6s  

2011-10-20 13:04:44 (76.2 KB/s) - `mongodb-linux-i686-2.0.0.tgz' saved [37949452/37949452]

[root@sunrise16 setup]#

 

[root@sunrise16 setup]# tar zxf mongodb-linux-i686-2.0.0.tgz
[root@sunrise16 setup]# cd mongodb-linux-i686-2.0.0
[root@sunrise16 mongodb-linux-i686-2.0.0]# ls
bin  GNU-AGPL-3.0  README  THIRD-PARTY-NOTICES
[root@sunrise16 mongodb-linux-i686-2.0.0]# cd bin
[root@sunrise16 bin]# ls
bsondump  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongorestore  mongos  mongosniff  mongostat  mongotop

 

启动mongod

[root@sunrise16 bin]# ./mongod
./mongod --help for help and startup options
Thu Oct 20 13:05:23
Thu Oct 20 13:05:23 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 20 13:05:23
Thu Oct 20 13:05:23 [initandlisten] MongoDB starting : pid=29178 port=27017 dbpath=/data/db/ 32-bit host=sunrise16
Thu Oct 20 13:05:23 [initandlisten]
Thu Oct 20 13:05:23 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 20 13:05:23 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 20 13:05:23 [initandlisten] **       with --journal, the limit is lower
Thu Oct 20 13:05:23 [initandlisten]
Thu Oct 20 13:05:23 [initandlisten] db version v2.0.0, pdfile version 4.5
Thu Oct 20 13:05:23 [initandlisten] git version: 695c67dff0ffc361b8568a13366f027caa406222
Thu Oct 20 13:05:23 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Thu Oct 20 13:05:23 [initandlisten] options: {}
Thu Oct 20 13:05:23 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Thu Oct 20 13:05:23 dbexit:
Thu Oct 20 13:05:23 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 20 13:05:23 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 20 13:05:23 [initandlisten] shutdown: going to close sockets...
Thu Oct 20 13:05:23 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 20 13:05:23 [initandlisten] shutdown: closing all files...
Thu Oct 20 13:05:23 [initandlisten] closeAllFiles() finished
Thu Oct 20 13:05:23 dbexit: really exiting now
[root@sunrise16 bin]#

根据屏幕上输出的信息,克制没有创建/data/db/目录。

 

[root@sunrise16 bin]# mkdir -p /data/db/
[root@sunrise16 bin]# ./mongod         
./mongod --help for help and startup options
Thu Oct 20 13:06:31
Thu Oct 20 13:06:31 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 20 13:06:31
Thu Oct 20 13:06:31 [initandlisten] MongoDB starting : pid=29510 port=27017 dbpath=/data/db/ 32-bit host=sunrise16
Thu Oct 20 13:06:31 [initandlisten]
Thu Oct 20 13:06:31 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 20 13:06:31 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 20 13:06:31 [initandlisten] **       with --journal, the limit is lower
Thu Oct 20 13:06:31 [initandlisten]
Thu Oct 20 13:06:31 [initandlisten] db version v2.0.0, pdfile version 4.5
Thu Oct 20 13:06:31 [initandlisten] git version: 695c67dff0ffc361b8568a13366f027caa406222
Thu Oct 20 13:06:31 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Thu Oct 20 13:06:31 [initandlisten] options: {}
Thu Oct 20 13:06:31 [websvr] admin web console waiting for connections on port 28017
Thu Oct 20 13:06:31 [initandlisten] waiting for connections on port 27017

 

Ctrl+C
Thu Oct 20 13:06:40 got kill or ctrl c or hup signal 2 (Interrupt), will terminate after current cmd ends
Thu Oct 20 13:06:40 [interruptThread] now exiting
Thu Oct 20 13:06:40 dbexit:
Thu Oct 20 13:06:40 [interruptThread] shutdown: going to close listening sockets...
Thu Oct 20 13:06:40 [interruptThread] closing listening socket: 5
Thu Oct 20 13:06:40 [interruptThread] closing listening socket: 6
Thu Oct 20 13:06:40 [interruptThread] closing listening socket: 7
Thu Oct 20 13:06:40 [interruptThread] removing socket file: /tmp/mongodb-27017.sock
Thu Oct 20 13:06:40 [interruptThread] shutdown: going to flush diaglog...
Thu Oct 20 13:06:40 [interruptThread] shutdown: going to close sockets...
Thu Oct 20 13:06:40 [interruptThread] shutdown: waiting for fs preallocator...
Thu Oct 20 13:06:40 [interruptThread] shutdown: closing all files...
Thu Oct 20 13:06:40 [interruptThread] closeAllFiles() finished
Thu Oct 20 13:06:40 [interruptThread] shutdown: removing fs lock...
Thu Oct 20 13:06:40 dbexit: really exiting now
[root@sunrise16 bin]#

 

 

[root@sunrise16 bin]# ./mongod --journal
Thu Oct 20 13:06:50 [initandlisten] MongoDB starting : pid=29519 port=27017 dbpath=/data/db/ 32-bit host=sunrise16
Thu Oct 20 13:06:50 [initandlisten]
Thu Oct 20 13:06:50 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 20 13:06:50 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 20 13:06:50 [initandlisten] **       with --journal, the limit is lower
Thu Oct 20 13:06:50 [initandlisten]
Thu Oct 20 13:06:50 [initandlisten] db version v2.0.0, pdfile version 4.5
Thu Oct 20 13:06:50 [initandlisten] git version: 695c67dff0ffc361b8568a13366f027caa406222
Thu Oct 20 13:06:50 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Thu Oct 20 13:06:50 [initandlisten] options: { journal: true }
Thu Oct 20 13:06:50 [initandlisten] journal dir=/data/db/journal
Thu Oct 20 13:06:50 [initandlisten] recover : no journal files present, no recovery needed
Thu Oct 20 13:06:51 [initandlisten] preallocateIsFaster=true 7.24
Thu Oct 20 13:06:51 [initandlisten] preallocateIsFaster=true 7.26
Thu Oct 20 13:06:53 [initandlisten] preallocateIsFaster=true 7.22
Thu Oct 20 13:06:53 [initandlisten] preallocating a journal file /data/db/journal/prealloc.0
Thu Oct 20 13:07:58 [initandlisten] preallocating a journal file /data/db/journal/prealloc.1
                220200960/268435456     82%
Thu Oct 20 13:09:03 [initandlisten] preallocating a journal file /data/db/journal/prealloc.2
                146800640/268435456     54%
                188743680/268435456     70%
                251658240/268435456     93%
Thu Oct 20 13:10:08 [initandlisten] waiting for connections on port 27017
Thu Oct 20 13:10:08 [websvr] admin web console waiting for connections on port 28017
Thu Oct 20 13:11:08 [clientcursormon] mem (MB) res:12 virt:102 mapped:0

 

下面的日志内容是在启动一个mongo客户端之后产生的。
Thu Oct 20 13:11:50 [initandlisten] connection accepted from 127.0.0.1:36877 #1
Thu Oct 20 13:14:41 [FileAllocator] allocating new datafile /data/db/test.ns, filling with zeroes...
Thu Oct 20 13:14:41 [FileAllocator] creating directory /data/db/_tmp
Thu Oct 20 13:14:45 [FileAllocator] done allocating datafile /data/db/test.ns, size: 16MB,  took 4.083 secs
Thu Oct 20 13:14:45 [FileAllocator] allocating new datafile /data/db/test.0, filling with zeroes...
Thu Oct 20 13:14:49 [FileAllocator] done allocating datafile /data/db/test.0, size: 16MB,  took 4.07 secs
Thu Oct 20 13:14:49 [FileAllocator] allocating new datafile /data/db/test.1, filling with zeroes...
Thu Oct 20 13:14:49 [conn1] build index test.foo { _id: 1 }
Thu Oct 20 13:14:49 [conn1] build index done 0 records 0.361 secs
Thu Oct 20 13:14:51 [conn1] insert test.foo 10673ms
Thu Oct 20 13:14:57 [FileAllocator] done allocating datafile /data/db/test.1, size: 32MB,  took 8.389 secs
Thu Oct 20 13:15:17 [clientcursormon] mem (MB) res:13 virt:199 mapped:32
Thu Oct 20 13:16:46 [FileAllocator] allocating new datafile /data/db/mydb.ns, filling with zeroes...
Thu Oct 20 13:16:51 [FileAllocator] done allocating datafile /data/db/mydb.ns, size: 16MB,  took 4.262 secs
Thu Oct 20 13:16:51 [FileAllocator] allocating new datafile /data/db/mydb.0, filling with zeroes...
Thu Oct 20 13:16:55 [FileAllocator] done allocating datafile /data/db/mydb.0, size: 16MB,  took 4.06 secs
Thu Oct 20 13:16:55 [FileAllocator] allocating new datafile /data/db/mydb.1, filling with zeroes...
Thu Oct 20 13:16:55 [conn1] build index mydb.things { _id: 1 }
Thu Oct 20 13:16:55 [conn1] build index done 0 records 0 secs
Thu Oct 20 13:16:55 [conn1] insert mydb.things 8330ms
Thu Oct 20 13:17:03 [FileAllocator] done allocating datafile /data/db/mydb.1, size: 32MB,  took 8.142 secs
Thu Oct 20 13:17:23 [clientcursormon] mem (MB) res:13 virt:263 mapped:64
Thu Oct 20 13:22:23 [clientcursormon] mem (MB) res:13 virt:263 mapped:64
Thu Oct 20 13:24:08 [conn1] end connection 127.0.0.1:36877

Ctrl+C
Thu Oct 20 13:24:31 got kill or ctrl c or hup signal 2 (Interrupt), will terminate after current cmd ends
Thu Oct 20 13:24:31 [interruptThread] now exiting
Thu Oct 20 13:24:31 dbexit:
Thu Oct 20 13:24:31 [interruptThread] shutdown: going to close listening sockets...
Thu Oct 20 13:24:31 [interruptThread] closing listening socket: 6
Thu Oct 20 13:24:31 [interruptThread] closing listening socket: 7
Thu Oct 20 13:24:31 [interruptThread] closing listening socket: 8
Thu Oct 20 13:24:31 [interruptThread] removing socket file: /tmp/mongodb-27017.sock
Thu Oct 20 13:24:31 [interruptThread] shutdown: going to flush diaglog...
Thu Oct 20 13:24:31 [interruptThread] shutdown: going to close sockets...
Thu Oct 20 13:24:31 [interruptThread] shutdown: waiting for fs preallocator...
Thu Oct 20 13:24:31 [interruptThread] shutdown: lock for final commit...
Thu Oct 20 13:24:31 [interruptThread] shutdown: final commit...
Thu Oct 20 13:24:31 [interruptThread] shutdown: closing all files...
Thu Oct 20 13:24:31 [interruptThread] closeAllFiles() finished
Thu Oct 20 13:24:31 [interruptThread] journalCleanup...
Thu Oct 20 13:24:31 [interruptThread] removeJournalFiles
Thu Oct 20 13:24:31 [interruptThread] shutdown: removing fs lock...
Thu Oct 20 13:24:31 dbexit: really exiting now
[root@sunrise16 bin]#
[root@sunrise16 bin]#

 

 

使用mongo命令行工具访问MongoDB

[root@sunrise16 bin]# ./mongo
MongoDB shell version: 2.0.0
connecting to: test
Thu Oct 20 13:07:54 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
[root@sunrise16 bin]# ./mongo
MongoDB shell version: 2.0.0
connecting to: test
Thu Oct 20 13:08:19 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
[root@sunrise16 bin]# ./mongo
MongoDB shell version: 2.0.0
connecting to: test
Thu Oct 20 13:08:29 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed

 


[root@sunrise16 bin]# ./mongo
MongoDB shell version: 2.0.0
connecting to: test

> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        rs.help()                    help on replica set methods
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

 

下面的测试内容来自 http://www.mongodb.org/display/DOCS/Quickstart+Unix

> db.foo.save({x:1})
> db.foo.find()
{ "_id" : ObjectId("4e9fae4127ed834a3f8d3b1a"), "x" : 1 }

 

下面的测试内容来自 http://www.mongodb.org/display/DOCS/Tutorial
> use mydb
switched to db mydb
> j={name:'zhy'}
{ "name" : "zhy" }
> t={t:3}
{ "t" : 3 }
> db.things.save(j)
> db.things.save(t)
> db.things.find()
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
{ "_id" : ObjectId("4e9faecf27ed834a3f8d3b1c"), "t" : 3 }
> for (var i = 1; i <= 20; i++) db.things.save({x : 4, j : i});
> db.things.find()
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
{ "_id" : ObjectId("4e9faecf27ed834a3f8d3b1c"), "t" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b20"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b21"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b22"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b23"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b24"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b25"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b26"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b27"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b28"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b29"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2a"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2b"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2c"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2d"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2e"), "x" : 4, "j" : 18 }
has more
> it
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2f"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b30"), "x" : 4, "j" : 20 }
> it
no cursor
> var cursor = db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
{ "_id" : ObjectId("4e9faecf27ed834a3f8d3b1c"), "t" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b20"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b21"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b22"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b23"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b24"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b25"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b26"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b27"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b28"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b29"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2a"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2b"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2c"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2d"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2e"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2f"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b30"), "x" : 4, "j" : 20 }
> db.things.find().forEach(printjson);
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
{ "_id" : ObjectId("4e9faecf27ed834a3f8d3b1c"), "t" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b20"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b21"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b22"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b23"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b24"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b25"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b26"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b27"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b28"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b29"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2a"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2b"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2c"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2d"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2e"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2f"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b30"), "x" : 4, "j" : 20 }
> var cursor = db.things.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1f"), "x" : 4, "j" : 3 }
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b20"), "x" : 4, "j" : 4 }
> db.things.find({name:"mongo"}).forEach(printjson);
> db.things.find({name:"zhy"}).forEach(printjson);
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
> db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b20"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b21"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b22"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b23"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b24"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b25"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b26"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b27"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b28"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b29"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2a"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2b"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2c"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2d"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2e"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2f"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b30"), "x" : 4, "j" : 20 }
> db.things.find({x:4}, {j:true}).forEach(printjson);
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1d"), "j" : 1 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1e"), "j" : 2 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1f"), "j" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b20"), "j" : 4 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b21"), "j" : 5 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b22"), "j" : 6 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b23"), "j" : 7 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b24"), "j" : 8 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b25"), "j" : 9 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b26"), "j" : 10 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b27"), "j" : 11 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b28"), "j" : 12 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b29"), "j" : 13 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2a"), "j" : 14 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2b"), "j" : 15 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2c"), "j" : 16 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2d"), "j" : 17 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2e"), "j" : 18 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b2f"), "j" : 19 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b30"), "j" : 20 }
> printjson(db.things.findOne({name:"mongo"}));
null
> printjson(db.things.findOne({name:"zhy"}));
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
> var doc = db.things.findOne({_id:ObjectId("4c2209f9f3924d31102bd84a")});
> doc
null
> var doc = db.things.findOne({_id:ObjectId("4e9faf0f27ed834a3f8d3b28")});
> doc
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b28"), "x" : 4, "j" : 12 }
> db.things.find().limit(3);
{ "_id" : ObjectId("4e9faebe27ed834a3f8d3b1b"), "name" : "zhy" }
{ "_id" : ObjectId("4e9faecf27ed834a3f8d3b1c"), "t" : 3 }
{ "_id" : ObjectId("4e9faf0f27ed834a3f8d3b1d"), "x" : 4, "j" : 1 }
> quit
function () {
    return nativeHelper.apply(quit_, arguments);
}
> exit
bye
[root@sunrise16 bin]#

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值