$ ./mongo --helpMongoDB shell version: 1.5.3usage: ./mongo [options] [db address] [file names (ending in .js)]db address can be: foo foo database on local machine 192.169.0.5/foo foo database on 192.168.0.5 machine 192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999options: --shell run the shell after executing files --nodb don't connect to mongod on startup - no 'db address' arg expected --quiet be less chatty --port arg port to connect to --host arg server to connect to --eval arg evaluate javascript -u [ --username ] arg username for authentication -p [ --password ] arg password for authentication -h [ --help ] show this usage information --version show version information --ipv6 enable IPv6 support (disabled by default)file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
$ ./mongoMongoDB shell version: 1.5.3connecting to: testtype "help" for help> help help admin misc shell commands 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 use <db name> set current database to <db name> db.help() help on DB methods db.foo.help() help on collection methods 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 exit quit the mongo shell
$ ./mongoMongoDB shell version: 1.5.3connecting to: testtype "help" for help> show dbs // 查看当前数据库列表adminlocal> use blog // 切换到工作数据库switched to db blog> db // 当前数据库blog> for (var i = 0; i < 10; i++) db.users.save({name : "user" + i, age : i}) // 插入数据> show dbs // 数据库 blog 被创建adminbloglocal> show collections // 列表 users 被创建system.indexesusers> db.copyDatabase("blog", "blog2") // 复制数据库{ "ok" : true }> show dbs // 数据库 blog2 被创建adminblogblog2local> use blog2 // 切换到 blog2switched to db blog2> show collections // 查看集合列表system.indexesusers> db.users.find() // 查看被复制的数据{ "_id" : ObjectId("4c33f8fcecf2b9320ac2981a"), "name" : "user0", "age" : 0 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac2981b"), "name" : "user1", "age" : 1 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac2981c"), "name" : "user2", "age" : 2 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac2981d"), "name" : "user3", "age" : 3 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac2981e"), "name" : "user4", "age" : 4 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac2981f"), "name" : "user5", "age" : 5 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac29820"), "name" : "user6", "age" : 6 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac29821"), "name" : "user7", "age" : 7 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac29822"), "name" : "user8", "age" : 8 }{ "_id" : ObjectId("4c33f8fcecf2b9320ac29823"), "name" : "user9", "age" : 9 }> db.dropDatabase() // 删除数据库 blog2{ "dropped" : "blog2", "ok" : true }> show dbs // 确认数据库删除成功adminbloglocal> use blog // 切换回 blogswitched to db blog> db.users.drop() // 删除集合 userstrue> show collections // 确认集合被删除system.indexes> exitbye
server64$ ./mongoMongoDB shell version: 1.5.3connecting to: testtype "help" for help> use blogswitched to db blog> for (var i = 0; i < 10; i++) db.users.save({name : "user" + i, age : i})> use newsswitched to db news> for (var i = 0; i < 10; i++) db.articles.save({title : "title" + i})> show dbsadminbloglocalnews> exitbye
server32:$ ./mongoMongoDB shell version: 1.5.4connecting to: test> db.copyDatabase("blog", "blog", "192.168.1.202") // 从源服务器复制 blog 数据库{ "ok" : true }> show dbs // 复制成功adminbloglocal> use blogswitched to db blog> show collectionssystem.indexesusers> db.users.find(){ "_id" : ObjectId("4c33fadb15b7f104d297e644"), "name" : "user0", "age" : 0 }{ "_id" : ObjectId("4c33fadb15b7f104d297e645"), "name" : "user1", "age" : 1 }{ "_id" : ObjectId("4c33fadb15b7f104d297e646"), "name" : "user2", "age" : 2 }{ "_id" : ObjectId("4c33fadb15b7f104d297e647"), "name" : "user3", "age" : 3 }{ "_id" : ObjectId("4c33fadb15b7f104d297e648"), "name" : "user4", "age" : 4 }{ "_id" : ObjectId("4c33fadb15b7f104d297e649"), "name" : "user5", "age" : 5 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64a"), "name" : "user6", "age" : 6 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64b"), "name" : "user7", "age" : 7 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64c"), "name" : "user8", "age" : 8 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64d"), "name" : "user9", "age" : 9 }> use newsswitched to db news> db.cloneDatabase("192.168.1.202") // 从源服务器克隆当前数据库(news){ "ok" : true }> show dbsadminbloglocalnews> show collectionsarticlessystem.indexes> db.articles.find(){ "_id" : ObjectId("4c33fb6215b7f104d297e64e"), "title" : "title0" }{ "_id" : ObjectId("4c33fb6215b7f104d297e64f"), "title" : "title1" }{ "_id" : ObjectId("4c33fb6215b7f104d297e650"), "title" : "title2" }{ "_id" : ObjectId("4c33fb6215b7f104d297e651"), "title" : "title3" }{ "_id" : ObjectId("4c33fb6215b7f104d297e652"), "title" : "title4" }{ "_id" : ObjectId("4c33fb6215b7f104d297e653"), "title" : "title5" }{ "_id" : ObjectId("4c33fb6215b7f104d297e654"), "title" : "title6" }{ "_id" : ObjectId("4c33fb6215b7f104d297e655"), "title" : "title7" }{ "_id" : ObjectId("4c33fb6215b7f104d297e656"), "title" : "title8" }{ "_id" : ObjectId("4c33fb6215b7f104d297e657"), "title" : "title9" }> exitbye
> use adminswitched to db admin> dbadmin> blog = db.getSisterDB("blog")blog> blog.users.insert({name : "abc"})> blog.users.find({name : "abc"}){ "_id" : ObjectId("4c3419b0492aa4cfbec11895"), "name" : "abc" }
> use adminswitched to db admin> db.runCommand({fsync : 1}){ "numFiles" : 6, "ok" : true }> db.runCommand({fsync : 1, async : true}){ "numFiles" : 6, "ok" : true }
$ ./mongoMongoDB shell version: 1.5.3connecting to: testtype "help" for help> use blogswitched to db blog> admin = db.getSisterDB("admin")admin> admin.runCommand({fsync : 1, lock : 1}) // 锁定{ "info" : "now locked against writes, use db.$cmd.sys.unlock.findOne() to unlock", "ok" : true}> db.users.find() // 读操作正常{ "_id" : ObjectId("4c33fadb15b7f104d297e644"), "name" : "user0", "age" : 0 }{ "_id" : ObjectId("4c33fadb15b7f104d297e645"), "name" : "user1", "age" : 1 }{ "_id" : ObjectId("4c33fadb15b7f104d297e646"), "name" : "user2", "age" : 2 }{ "_id" : ObjectId("4c33fadb15b7f104d297e647"), "name" : "user3", "age" : 3 }{ "_id" : ObjectId("4c33fadb15b7f104d297e648"), "name" : "user4", "age" : 4 }{ "_id" : ObjectId("4c33fadb15b7f104d297e649"), "name" : "user5", "age" : 5 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64a"), "name" : "user6", "age" : 6 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64b"), "name" : "user7", "age" : 7 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64c"), "name" : "user8", "age" : 8 }{ "_id" : ObjectId("4c33fadb15b7f104d297e64d"), "name" : "user9", "age" : 9 }> db.users.save({name : "xyz" }) // 写操作被阻塞,等待 ...
> use adminswitched to db admin> db.$cmd.sys.unlock.findOne(){ "ok" : 1, "info" : "unlock requested" }
上一篇 Apache与PHP的整合过程