Commands commonly used in MongoDB
If you want to see which database the current connection is under, you can type db directly.
> db
Admin
I want to switch to the test database.
> use test
switched to db test
> db
Test To see which tables are under test, or collection, you can enter
> show collections
system.indexes
user If you want to know which commands mongodb supports, you can enter help directly.
> help
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 set curent database to
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
If you want to know which methods are supported by the current database:
> db.help();
DB methods:
db.addUser(username, password) Add database authorized user
db.auth(username, password)
db.cloneDatabase(fromhost) clone database
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) Copy database
db.createCollection(name, { size : ..., capped : ..., max : ... } ) Create table
db.currentOp() displays the current operation in the db
db.dropDatabase() deletes the current database
db.eval_r(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames() Gets the table names of the current database
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()
db.getReplicationInfo()
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() kills the current operation in the db
db.printCollectionStats() Print status information for each table
db.printReplicationInfo() Print replication status information for the primary database
db.printSlaveReplicationInfo() Print replication status information from the database
db.printShardingStatus() Print the status information of the fragment
db.removeUser(username) Delete database user
db.repairDatabase() Repair database
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
db.setProfilingLevel(level) 0=off 1=slow 2=all
db.shutdownServer()
db.version() current version of the server