MongoDB shell version: 2.2.1connecting to: test> helpdb.help() help on db methodsdb.mycoll.help() help on collection methodssh.help() sharding helpersrs.help() replica set helpershelp admin administrative helphelp connect connecting to a db helphelp keys key shortcutshelp misc misc things to knowhelp mr mapreduceshow dbs show database namesshow collections show collections in current databaseshow users show users in current databaseshow profile show most recent system.profile entries with time >= 1msshow logs show the accessible logger namesshow log [name] prints out the last segment of log in memory, 'global' is defaultuseset current database
db.foo.find() list objects in collection foodb.foo.find( { a : 1 } ) list objects in foo where a == 1it result of the last line evaluated; use to further iterateDBQuery.shellBatchSize = x set default number of items to display on shellexit quit the mongo shell> db.help() DB methods: db.addUser(username, password[, readOnly=false]) //添加用户 db.adminCommand(nameOrDocument) //switches to 'admin' db, and runs command [ just calls db.runCommand(...) ] db.auth(username, password) //设置数据库连接验证 db.cloneDatabase(fromhost) //从目标服务器克隆一个数据库 db.commandHelp(name) //returns the help for the command db.copyDatabase(fromdb, todb, fromhost) //复制数据库fromdb---源数据库名称,todb---目标数据库名称,fromhost---源数据库服务器地址 db.createCollection(name, { size : ..., capped : ..., max : ... } ) //创建集合 db.currentOp() //displays currently executing operations in the db db.dropDatabase() //删除当前数据库 db.eval(func, args) //run code server-side db.fsyncLock() //flush data to disk and lock server for backups db.fsyncUnlock() //unlocks server following a db.fsyncLock() 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 queries on a replication slave server 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.hostInfo() //get details about the server's host db.isMaster() //check replica primary status db.killOp(opid) //kills the current operation in the db db.listCommands() //lists all the db commands db.loadServerScripts() //loads all the scripts in db.system.js db.logout() db.printCollectionStats() db.printReplicationInfo() db.printShardingStatus() db.printSlaveReplicationInfo() 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,推荐查看:http://www.studyday.net/2011/06/218) 0=off 1=slow 2=all db.setVerboseShell(flag) //display extra information in shell output db.shutdownServer() //关闭数据库 db.stats() //查看数据库状态db.version() //current version of the server> db.test.help()DBCollection helpdb.test.find().help() //show DBCursor helpdb.test.count() //统计集合文档数db.test.copyTo(newColl) //duplicates collection by copying all documents to newColl; no indexes are copied.db.test.convertToCapped(maxBytes) //calls {convertToCapped:'test', size:maxBytes}} commanddb.test.dataSize()db.test.distinct( key ) //eg. db.test.distinct( 'x' )db.test.drop() //删除集合db.test.dropIndex(name) //删除指定索引db.test.dropIndexes() //删除所有索引db.test.ensureIndex(keypattern[,options]) //options is an object with these possible fields: name, unique, dropDupsdb.test.reIndex() //重建索引db.test.find([query],[fields]) //query is an optional query filter. fields is optional set of fields to return. e.g. db.test.find( {x:77} , {name:1, x:1} )db.test.find(...).count()db.test.find(...).limit(n)db.test.find(...).skip(n)db.test.find(...).sort(...)db.test.findOne([query])db.test.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )db.test.getDB() //get DB object associated with collectiondb.test.getIndexes()db.test.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )db.test.insert(obj)db.test.mapReduce( mapFunction , reduceFunction , )db.test.remove(query)db.test.renameCollection( newName , ) //renames the collection.db.test.runCommand( name , ) //runs a db command with the given name where the first param is the collection namedb.test.save(obj)db.test.stats()db.test.storageSize() //includes free space allocated to this collectiondb.test.totalIndexSize() //size in bytes of all the indexesdb.test.totalSize() //storage allocated for all data and indexesdb.test.update(query, object[, upsert_bool, multi_bool]) //instead of two flags, you can pass an object with fields: upsert, multidb.test.validate( ) //SLOWdb.test.getShardVersion() //only for use with shardingdb.test.getShardDistribution() //prints statistics about data distribution in the clusterdb.test.getSplitKeysForChunks( ) //calculates split points over all chunks and returns splitter function