mongo-help功能
mongo提供了help功能,可以帮助我查看一些命令。下面是示例:
> 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
> db
admin
> 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
> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([secs]) step down as primary (momentarily) (disconnects)
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() shorthand for db.getMongo().setSlaveOk()
db.isMaster() check who is primary
reconfiguration helpers disconnect from the database so the shell will display
an error, even if the command succeeds.
see also http://<mongod_host>:28017/_replSet for additional diagnostic info
> db
admin
> show collections
settings
system.indexes
> db.settings.help()
DBCollection help
db.settings.find().help() - show DBCursor help
db.settings.count()
db.settings.dataSize()
db.settings.distinct( key ) - eg. db.settings.distinct( 'x' )
db.settings.drop() drop the collection
db.settings.dropIndex(name)
db.settings.dropIndexes()
db.settings.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.settings.reIndex()
db.settings.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.settings.find( {x:77} , {name:1, x:1} )
db.settings.find(...).count()
db.settings.find(...).limit(n)
db.settings.find(...).skip(n)
db.settings.find(...).sort(...)
db.settings.findOne([query])
db.settings.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.settings.getDB() get DB object associated with collection
db.settings.getIndexes()
db.settings.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.settings.mapReduce( mapFunction , reduceFunction , <optional params> )
db.settings.remove(query)
db.settings.renameCollection( newName , <dropTarget> ) renames the collection.
db.settings.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.settings.save(obj)
db.settings.stats()
db.settings.storageSize() - includes free space allocated to this collection
db.settings.totalIndexSize() - size in bytes of all the indexes
db.settings.totalSize() - storage allocated for all data and indexes
db.settings.update(query, object[, upsert_bool, multi_bool])
db.settings.validate() - SLOW
db.settings.getShardVersion() - only for use with sharding
>
如果在输入函数的时候,不加上左右括号会怎么样呢?注意看mongo提示出来的是这个函数的JS源码。通过这个小技巧,你可以了解这个函数的实现方法,以及它的参数及顺序:
> db.settings.insert
function (obj, _allow_dot) {
if (!obj) {
throw "no object passed to insert!";
}
if (!_allow_dot) {
this._validateForStorage(obj);
}
if (typeof obj._id == "undefined") {
var tmp = obj;
obj = {_id:new ObjectId};
for (var key in tmp) {
obj[key] = tmp[key];
}
}
this._mongo.insert(this._fullName, obj);
this._lastID = obj._id;
}