`

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
1
分享到:
评论
3 楼 hyneng 2012-08-20  
勇敢的核桃 写道
Thu Oct 20 13:08:29 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
出现了这个问题。如何解决的??
楼主没有贴出来

我随机启动会出这样的错,但直接启动没错,谁知是什么原因不
2 楼 codingstandards 2012-02-29  
勇敢的核桃 写道
Thu Oct 20 13:08:29 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
出现了这个问题。如何解决的??
楼主没有贴出来

首先确认服务端是否启动了,
然后是服务器是否初始化好了,
这两种情况都会报这个错
1 楼 勇敢的核桃 2012-02-27  
Thu Oct 20 13:08:29 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
出现了这个问题。如何解决的??
楼主没有贴出来

相关推荐

    mongodb 学习记录

    MongoDB 学习实例 记录

    mongodb学习记录

    mongodb学习记录,自己学习时的笔记,放到上页面,和大家分享、

    使用aggregate在MongoDB中查询重复数据记录的方法

    主要介绍了使用aggregate在MongoDB中查询重复数据记录的方法的相关资料,需要的朋友可以参考下

    NoSQL Manager for MongoDB 无限30天试用

    亲测可用, NoSQL Manager for MongoDB 2.7.x, 支持MongoDB 3.0, 下载运行 破解.bat即可

    studio3T无限试用,mongodb图形化管理工具

    studio3T无限试用,mongodb图形化管理工具,运行脚本就可以试用30天,到期后再运行,可以无限试用,脚本可以放到自启动里面,这样就可以自动运行

    记录MongoDB管理操作

    将房产系统在一年多来使用的mongodb管理方面上做了一些基本管理操作的记录,包括了安装启动,还有对mongodb进行监控,备份和修复,安全认证等等,当然了,还没有涉及到分片

    mongodb-license.txt

    mongodb drive mongodb 驱动 试用idea等 用于mongodb数据连接 dbeaver数据库链接。开放资源

    mongodb随机获取一条记录实例

    介绍php7.0以上使用mongdb的基本方法,并提供mongodb随机获取一条记录的测试实例。

    Ruby-MongoDBLoggerRails的MongoDB记录器

    MongoDB Logger - Rails的MongoDB记录器

    MongoDB笔记.docx

    一、MongoDB简介 3 二、MongoDB结构 3 二、MongoDB 数据库关系型(这里并不是值关系型数据库的关系) 3 1、MongoDB一对一关系型 3 2、MongoDB一对多关系型 4 3、MongoDB多对多关系型 4 三、创建数据库(mongodb_test...

    Linux安装mongodb客户端

    sudo vim /etc/yum.repos.d/mongodb-org-4.2.repo 写入: [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpg...

    基于MongoDB的日志系统Mongodb-Log.zip

    MongoDB 的 Capped Collection是一个天生的日志系统,MongoDB自己的oplog就是用它来存储的,Capped Collection的特点是可以指定Collection的大小,当记录总大小超过设定大小后,老的数据会被自动抹掉用于存储新的...

    如何安装MongoDB 如何使用MongoDB

    本课程是一套关于MongoDB应用开发的实战性教程,名为《深入浅出MongoDB应用实战开发(基础、开发指南、系统管理、集群及系统架构)》,教程侧重于讲解MongoDB的常用特性及高级特性,从实际开发的角度出发对MongoDB...

    MongoDB应用设计模式

    资源名称:MongoDB应用设计模式内容简介:无论是在构建社交媒体网站,还是在开发一个仅在内部使用的企业应用程序,《MongoDB应用设计模式》展示了MongoDB需要解决的商业问题之间的连接。你将学到如何把MongoDB设计...

    MongoDB图形化管理工具 MongoDB Compass

    MongoDB图形化管理工具 MongoDB Compass

    五、MongoDB 学习PPT

    MongoDB 学习PPT

    【BAT必备】MongoDB面试题

    【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT...

    MongoDB教程基础入门

    教程名称:MongoDB教程基础入门 课程目录:【】MongoDB教程基础入门-代码【】MongoDB教程基础入门01第一讲上【】MongoDB教程基础入门02第一讲下【】MongoDB教程基础入门03第二讲上【】MongoDB教程基础入门04第二讲...

    mongodb-linux-x86_64-4.0.18.tgz

    mv mongodb-linux-x86_64-4.0.18 mongodb 3、进入 mongodb 目录创建目录 db 和 logs cd /usr/local/mongodb mkdir db mkdir logs 4、进入到 bin 目录下,编辑 mongodb.conf 文件,内容如下: dbpath=/usr/local/...

    imaxue#progress#08-mongodb记录1

    mongodb记录1.show collections 看所有集合5.删除。

Global site tag (gtag.js) - Google Analytics