MongoDB命令帮助系统
在安装MongoDB后,启动服务器进程(mongod),可以通过在客户端命令mongo实现对MongoDB的管理和监控。看一下MongoDB的命令帮助系统:
root@dev2:~# mongo MongoDB shell version: 1.8.3 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 connect connecting to a db help help admin administrative help help misc misc things to know help mr mapreduce help 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 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
这是MongoDB最顶层的命令列表,主要告诉我们管理数据库相关的一些抽象的范畴:数据库操作帮助、集合操作帮助、管理帮助。如果你想了解数据库操作更详细的帮助命令,可以直接使用db.help(),如下所示:
> db.help() DB methods: db.addUser(username, password[, readOnly=false]) db.auth(username, password) db.cloneDatabase(fromhost) db.commandHelp(name) returns the help for the command db.copyDatabase(fromdb, todb, fromhost) db.createCollection(name, { size : ..., capped : ..., max : ... } ) db.currentOp() displays the current operation in the db db.dropDatabase() db.eval(func, args) run code server-side db.getCollection(cname) same as db['cname'] or db.cname db.getCollectionNames() db.getLastError() - just returns the err msg string db.getLastErrorObj() - return full status object db.getMongo() get the server connection object db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair db.getName() db.getPrevError() db.getProfilingLevel() - deprecated db.getProfilingStatus() - returns if profiling is on and slow threshold db.getReplicationInfo() db.getSiblingDB(name) get the db at the same server as this one db.isMaster() check replica primary status db.killOp(opid) kills the current operation in the db db.listCommands() lists all the db commands db.printCollectionStats() db.printReplicationInfo() db.printSlaveReplicationInfo() db.printShardingStatus() db.removeUser(username) db.repairDatabase() db.resetError() db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 } db.serverStatus() db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all db.shutdownServer() db.stats() db.version() current version of the server db.getMongo().setSlaveOk() allow queries on a replication slave server
对数据库进行管理和操作的基本命令,可以从上面获取到。如果想要得到更多,而且每个命令的详细用法,可以使用上面列出的db.listCommands()查询。
另一个比较基础的是对指定数据库的集合进行操作、管理和监控,可以通过查询db.mycoll.help()获取到:
> db.mycoll.help() DBCollection help db.mycoll.find().help() - show DBCursor help db.mycoll.count() db.mycoll.dataSize() db.mycoll.distinct( key ) - eg. db.mycoll.distinct( 'x' ) db.mycoll.drop() drop the collection db.mycoll.dropIndex(name) db.mycoll.dropIndexes() db.mycoll.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups db.mycoll.reIndex() db.mycoll.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return. e.g. db.mycoll.find( {x:77} , {name:1, x:1} ) db.mycoll.find(...).count() db.mycoll.find(...).limit(n) db.mycoll.find(...).skip(n) db.mycoll.find(...).sort(...) db.mycoll.findOne([query]) db.mycoll.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } ) db.mycoll.getDB() get DB object associated with collection db.mycoll.getIndexes() db.mycoll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) db.mycoll.mapReduce( mapFunction , reduceFunction , <optional params> ) db.mycoll.remove(query) db.mycoll.renameCollection( newName , <dropTarget> ) renames the collection. db.mycoll.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name db.mycoll.save(obj) db.mycoll.stats() db.mycoll.storageSize() - includes free space allocated to this collection db.mycoll.totalIndexSize() - size in bytes of all the indexes db.mycoll.totalSize() - storage allocated for all data and indexes db.mycoll.update(query, object[, upsert_bool, multi_bool]) db.mycoll.validate() - SLOW db.mycoll.getShardVersion() - only for use with sharding
有关数据库和集合管理的相关命令,是最基础和最常用的,如集合查询、索引操作等。