`

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学习视频.zip

    3. **MongoDB Manager Pro**:"mongodbmanagerpro_inst30day.exe"可能是MongoDB管理工具的试用版本安装程序。这样的工具通常提供图形用户界面,帮助用户更方便地管理MongoDB实例,包括监控、备份、恢复和性能优化等...

    mongoDB管理软件

    1. 数据浏览:用户可以通过MongoVUE清晰地查看MongoDB数据库中的集合(相当于关系数据库中的表),包括文档(记录)的结构和内容。它支持排序、过滤和搜索,方便快速定位和理解数据。 2. 数据编辑与插入:MongoVUE...

    mongodb compass1.15.1官网版 免安装直接使用

    MongoDB Compass 1.15.1是这个系列的一个版本,它无需安装,可以直接运行,这在许多情况下非常便捷,特别是对于那些希望快速试用或在不同环境中使用的用户。 MongoDB是一款开源的文档型数据库,它是NoSQL数据库的...

    NoSQL Manager for MongoDB 3.8.2.4 管理工具

    它支持对数据进行增删改查操作,如添加新记录、编辑现有记录、删除无用数据,并且提供了强大的查询构建器,允许用户通过简单的拖放或编写MongoDB查询语法来筛选和排序数据。 其次,这款工具支持备份和恢复功能,这...

    nosqlbooster 试用清理

    3.对于4.X及以下版本,可每30天清理一次,重置试用期限,对于5.X以上版本,官方开始增加了网络验证,每次运行后,会同步服务端记录的试用日期回本地,需要每次试用完毕后运行清理一次,或者选择端外网试用;...

    hospital-management-system:用Vuejs,Expressjs,Nodejs,Mongodb制作的医院管理系统。 小型医院和诊所的理想选择

    使用Heroku演示试用医院管理系统链接到演示----&gt; 演示应用程序的登录ID 以管理员身份登录 用户名:管理员 电话号码:9876543210 密码:密码 以接待员身份登录 用户名:接待员 电话号码:1122334455 密码:密码...

    peak-everest-furniture:使用React,Express.js和MongoDB Atlas构建的面向小型企业的完整电子商务解决方案

    试用我们的Web应用程序: 描述 使用React,Express.js和MongoDB Atlas构建的面向小型企业的完整电子商务解决方案。 该应用程序包括诸如动态更新购物车,用于客户信息和交易记录的数据库以及用于管理的管理仪表板之...

    一个企业网站源代码,欢迎下载试用。

    本资源提供了一个企业网站的源代码,虽然发布者并未亲自试用,但根据描述,代码质量较高,适合开发者进行学习、研究或二次开发。 在深入探讨之前,首先理解什么是源代码。源代码,也称为编程源码,是程序员使用编程...

    BudgetMonitor:作为 AngularJSExpressMongo 的试用项目管理您的预算

    "BudgetMonitor" 是一个基于 AngularJS、Express 和 MongoDB 的应用程序,用于试用和实践项目管理,特别是关注个人或组织的预算管理。AngularJS 是前端框架,Express 是后端框架,MongoDB 是数据库系统,三者结合...

    debugbar-v7.0及注册码

    - `Core.Services.DebugBar.v7.0.Keymaker.zip`文件包含了注册码生成器,这在软件试用期结束后用于激活正式版。 - 解压缩`Keymaker.zip`,运行其中的程序生成适用于DebugBar-v7.0的注册码。 - 在DebugBar的激活...

    a5m2_2.14.0_x64

    6. **history.txt**:这可能是一个日志文件或版本历史记录,记录了软件的更新和改动。 综合以上信息,"A5M2_2.14.0_x64"是一个强大的跨数据库管理工具,支持多种数据库类型,如MySQL和MongoDB,适用于个人学习和...

    万岳教育源码,教育系统源码,培训机构源码.zip

    7. **学习进度追踪**:系统记录用户的学习轨迹,包括已观看的视频、完成的作业等,帮助用户了解自身学习进度,并为用户提供个性化推荐。 8. **支付与订单管理**:对于收费课程,系统需集成支付接口,如支付宝、微信...

    茄子图书订阅系统!!!!

    这通常涉及到关系型数据库如MySQL或非关系型数据库如MongoDB的使用。数据库设计应遵循正常化原则,确保数据的一致性和完整性。此外,高效的查询优化和索引策略也是必不可少的,以支持快速的数据检索。 2. **Web开发...

    基于微信小程序的图书馆综合服务系统的设计与实现 (2).docx

    - **后端技术栈**:可选用Node.js结合Express框架,数据库则可以选择MySQL或MongoDB等。 - **服务器部署**:考虑使用阿里云、腾讯云等云服务商提供的服务器资源进行部署。 #### 四、关键技术点分析 1. **用户认证...

    数据库navicate 12 最新说明

    对于试用版用户,部分高级功能可能受到限制。 **最终用户许可协议** 在使用Navicat之前,用户需要阅读并同意最终用户许可协议(EULA)。该协议详细规定了软件的使用权限及限制条件。 #### 第二章:用户界面 **主...

    泡泡留言板测试版.zip

    10. **错误处理和日志记录**:软件应具有良好的错误处理机制,能够捕获并记录异常,帮助开发者快速定位和解决问题。 总的来说,“泡泡留言板测试版.zip”包含了创建和运行一个在线留言板系统所需的各种技术和资源,...

    simplecrm:学习使用meteorjs构建一个简单的CRM系统

    在MeteorJS中,我们可以使用MongoDB的文档型数据模型来存储客户信息、销售记录等数据。MongoDB的灵活性使得数据结构可以随着业务需求的变化而调整,非常适合敏捷开发。 在构建CRM系统时,我们需要创建几个核心组件...

    基于小程序的图书馆座位预约系统设计与实现.zip

    3. 数据库设计:MySQL或MongoDB等数据库存储用户信息、座位状态、预约记录等数据。 三、主要功能模块 1. 用户模块:用户注册、登录、个人信息管理,包括身份验证(如学生证号)、密码找回等功能。 2. 座位查询:...

    demo.codec2i.net.zip_DEMO_codec2i 众筹_众筹

    数据库可能选择MySQL、PostgreSQL或MongoDB等,存储用户信息、项目数据和交易记录。 为了充分利用 codec2i,用户需要熟悉基本的平台操作,例如如何创建和编辑项目页面,如何设置筹款目标和回报,如何与支持者沟通,...

    完整版在线查询IP地址.e.rar

    在描述中同样提到了“完整版在线查询IP地址.e.rar”,这表明该压缩文件提供了完整的功能,而不是试用版、简化版或部分功能版。这意味着用户在解压并运行其中的程序后,应该能够获得全面的IP地址查询体验,包括但不...

Global site tag (gtag.js) - Google Analytics