chenzhou123520 发表于 2013-1-12 13:23:20

Mongodb常用命令介绍

查看命令的方式:
1.在shell中运行db.listCommands()
2.在浏览器中访问管理员接口:http://ipaddress:28017/_commands
 
下面介绍在Mongodb中最经常使用的命令,具体如下:
命令:buildInfo
格式:{"buildInfo":1}
介绍:管理专用命令,返回Mongodb服务器的版本号和主机的操作系统。
示例:

> db.runCommand({"buildInfo":1}){"version" : "2.0.6","gitVersion" : "e1c0cbc25863f6356aa4e31375add7bb49fb05bc","sysInfo" : "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_41","versionArray" : ,"bits" : 32,"debug" : false,"maxBsonObjectSize" : 16777216,"ok" : 1}命令:collStats
格式:{"collStats":collection}
介绍:返回指定集合的统计信息,包括数据大小、已分配的存储空间和索引的大小。
示例:

> db.runCommand({"collStats":"users"}){"ns" : "test.users","count" : 3,"size" : 508,"avgObjSize" : 169.33333333333334,"storageSize" : 4096,"numExtents" : 1,"nindexes" : 2,"lastExtentSize" : 4096,"paddingFactor" : 1.51,"flags" : 0,"totalIndexSize" : 16352,"indexSizes" : {"_id_" : 8176,"name_1" : 8176},"ok" : 1}命令:distinct
格式:{"distinct":collection,"key":key,"query":query}
介绍:列出指定集合中满足查询条件的文档的指定键的所有不同值
示例: 

> db.runCommand({"distinct":"foo","key":"name","query":{"age":{"$gt":20}}}){"values" : ["gongyong","chenzhou","yixin"],"stats" : {"n" : 4,"nscanned" : 6,"nscannedObjects" : 6,"timems" : 50,"cursor" : "BasicCursor"},"ok" : 1}命令:drop
格式:{"drop":collection}
介绍:删除集合的所有数据
示例: 

> db.bbb.save({"x":1,"y":2})#先往bbb中存一条记录> db.bbb.find() #查询bbb中的数据{ "_id" : ObjectId("5027d919831a10b0f6e61385"), "x" : 1, "y" : 2 }#使用drop命令删除bbb集合中的数据> db.runCommand({"drop":"bbb"}){"nIndexesWas" : 1,"msg" : "indexes dropped for collection","ns" : "test.bbb","ok" : 1}> db.bbb.find() #再次查询,结果为空命令:dropDatabase
格式:{"dropDatabase":1}
介绍:删除当前数据库中的所有数据
示例:略
 
命令:dropIndexes
格式:{"dropIndexes":collection,"index":name}
介绍:删除集合里面名称为name的索引,如果名称为"*",则删除全部索引。
示例:
> db.system.indexes.find(){ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.foo", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.users", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.games", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.blog.post", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.lists", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.math", "name" : "_id_" }{ "v" : 1, "key" : { "name" : 1 }, "ns" : "test.users", "name" : "name_1" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.map", "name" : "_id_" }{ "v" : 1, "key" : { "gps" : "2d" }, "ns" : "test.map", "name" : "gps_", "min" : -180, "max" : 181 }> db.runCommand({"dropIndexes":"users","index":"name_1"}){ "nIndexesWas" : 2, "ok" : 1 }> db.system.indexes.find(){ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.foo", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.users", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.games", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.blog.post", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.lists", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.math", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.map", "name" : "_id_" }{ "v" : 1, "key" : { "gps" : "2d" }, "ns" : "test.map", "name" : "gps_", "min" : -180, "max" : 181 }命令:findAndModify
格式:
介绍:查找并修改
示例: 

> db.foo.find({"name":"yixin"}){ "_id" : ObjectId("5027d84b831a10b0f6e61384"), "name" : "yixin", "age" : 23 }> db.runCommand({"findAndModify":"foo",... "query":{"name":"yixin"},... "update":{"$inc":{"age":1}}}) #把name为yixin的记录中age值加1{"lastErrorObject" : {"updatedExisting" : true,"n" : 1,"connectionId" : 2,"err" : null,"ok" : 1},"value" : {"_id" : ObjectId("5027d84b831a10b0f6e61384"),"name" : "yixin","age" : 23},"ok" : 1}> db.foo.find({"name":"yixin"}){ "_id" : ObjectId("5027d84b831a10b0f6e61384"), "name" : "yixin", "age" : 24 }命令:getLastError
格式:{"getLastError":1[ , "w":w[ , "wtimeout":timeout}
介绍:查看对本集合执行的最后一次操作的错误信息或者其它状态信息。在w台服务器复制集合的最后操作之前,这个命令会阻塞。
示例:
> db.runCommand({"getLastError":1}){ "n" : 0, "connectionId" : 2, "err" : null, "ok" : 1 }命令:isMaster
格式:{"isMaster":1}
介绍:检查本服务器是主服务器还是从服务器
示例:
> db.runCommand({"isMaster":1}){ "ismaster" : true, "maxBsonObjectSize" : 16777216, "ok" : 1 }命令:listCommands
格式:{"listCommands":1}
介绍:返回所有可以在服务器上运行的命令及相关信息。
示例: 返回结果太多,示例就省略了
 
命令:listDatabases
格式:{"listDatabases":1}
介绍:管理专用命令,列出服务器上所有的数据库
示例:
> db.runCommand({"listDatabases":1}){ "errmsg" : "access denied; use admin db", "ok" : 0 }#报错,提示访问被拒绝,要求必须使用admin数据库> use admin #切换到admin数据库switched to db admin> db.runCommand({"listDatabases":1}){"databases" : [{"name" : "test","sizeOnDisk" : 67108864,"empty" : false},{"name" : "results","sizeOnDisk" : 67108864,"empty" : false},{"name" : "admin","sizeOnDisk" : 1,"empty" : true},{"name" : "local","sizeOnDisk" : 1,"empty" : true}],"totalSize" : 134217728,"ok" : 1}命令:ping
格式:{"ping":1}
介绍:检查服务器链接是否正常。即便服务器上锁了,这条命令也会立刻返回
示例:

> db.runCommand({"ping":1}){ "ok" : 1 }
命令:renameCollection
格式:{"renameCollection":a,"to":b}
介绍:将集合a重命名为b,其中a和b都必须是完整的集合命名空间(例如"test.foo"代表test数据库中的foo集合)
示例:

> db.runCommand({"renameCollection":"foo","to":"foo_bak"}){ "errmsg" : "access denied; use admin db", "ok" : 0 }> use adminswitched to db admin> db.runCommand({"renameCollection":"foo","to":"foo_bak"}){"errmsg" : "exception: source namespace does not exist","code" : 10026,"ok" : 0}> db.runCommand({"renameCollection":"test.foo","to":"test.foo_bak"}){ "ok" : 1 }> db.foo_bak.find()> use testswitched to db test> db.foo_bak.find(){ "_id" : ObjectId("502149203e2ff9961cc7b555"), "name" : "chenzhou" }{ "_id" : ObjectId("502149353e2ff9961cc7b556"), "age" : 23 }{ "_id" : ObjectId("5021495c3e2ff9961cc7b557"), "name" : "gongyong", "age" : 26 }{ "_id" : ObjectId("50214ad63e2ff9961cc7b558"), "name" : "chenzhou", "age" : 22, "friends" : 500, "enemies" : 2 }{ "_id" : 123, "x" : 1 }{ "_id" : ObjectId("5027d84b831a10b0f6e61384"), "name" : "yixin", "age" : 24 }
命令:repairDatabase
格式:{"repairDatabase":1}
介绍:修复并压缩当前数据库,这个操作可能非常耗时。
示例:略
 

命令:serverStatus
格式:{"serverStatus":1}
介绍:返回这台服务器的管理统计信息。
示例:


> db.runCommand({"serverStatus":1}){"host" : "localhost.localdomain","version" : "2.0.6","process" : "mongod","uptime" : 3661,"uptimeEstimate" : 2987,"localTime" : ISODate("2012-08-12T17:07:05.076Z"),"globalLock" : {"totalTime" : 3659420009,"lockTime" : 609397,"ratio" : 0.00016652830188971076,"currentQueue" : {"total" : 0,"readers" : 0,"writers" : 0},"activeClients" : {"total" : 0,"readers" : 0,"writers" : 0}},"mem" : {"bits" : 32,"resident" : 45,"virtual" : 158,"supported" : true,"mapped" : 64},"connections" : {"current" : 1,"available" : 818},"extra_info" : {"note" : "fields vary by platform","heap_usage_bytes" : 491848,"page_faults" : 0},"indexCounters" : {"btree" : {"accesses" : 1,"hits" : 1,"misses" : 0,"resets" : 0,"missRatio" : 0}},"backgroundFlushing" : {"flushes" : 60,"total_ms" : 119,"average_ms" : 1.9833333333333334,"last_ms" : 0,"last_finished" : ISODate("2012-08-12T17:06:05.816Z")},"cursors" : {"totalOpen" : 0,"clientCursors_size" : 0,"timedOut" : 0},"network" : {"bytesIn" : 7312,"bytesOut" : 63515,"numRequests" : 102},"opcounters" : {"insert" : 2,"query" : 29,"update" : 1,"delete" : 0,"getmore" : 0,"command" : 75},"asserts" : {"regular" : 0,"warning" : 0,"msg" : 0,"user" : 1,"rollovers" : 0},"writeBacksQueued" : false,"ok" : 1}  
页: [1]
查看完整版本: Mongodb常用命令介绍