一、基本操作
1.显示数据库列表:show dbs
> show dbs
admin 0.000GB
local 0.000GB
wgq 0.000GB
2.切换当前数据库/创建数据库:use <db name>
> use test
switched to db test
3.显示数据库操作命令:db.help
> db.help
function () {
print("DB methods:");
print(
"\tdb.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]");
print("\tdb.auth(username, password)");
print("\tdb.cloneDatabase(fromhost)");
print("\tdb.commandHelp(name) returns the help for the command");
print("\tdb.copyDatabase(fromdb, todb, fromhost)");
print("\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )");
print("\tdb.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } )");
print("\tdb.createUser(userDocument)");
print("\tdb.currentOp() displays currently executing operations in the db");
print("\tdb.dropDatabase()");
print("\tdb.eval() - deprecated");
print("\tdb.fsyncLock() flush data to disk and lock server for backups");
print("\tdb.fsyncUnlock() unlocks server following a db.fsyncLock()");
print("\tdb.getCollection(cname) same as db['cname'] or db.cname");
print(
"\tdb.getCollectionInfos([filter]) - returns a list that contains the names and options" +
" of the db's collections");
print("\tdb.getCollectionNames()");
print("\tdb.getLastError() - just returns the err msg string");
print("\tdb.getLastErrorObj() - return full status object");
print("\tdb.getLogComponents()");
print("\tdb.getMongo() get the server connection object");
print("\tdb.getMongo().setSlaveOk() allow queries on a replication slave server");
print("\tdb.getName()");
print("\tdb.getPrevError()");
print("\tdb.getProfilingLevel() - deprecated");
print("\tdb.getProfilingStatus() - returns if profiling is on and slow threshold");
print("\tdb.getReplicationInfo()");
print("\tdb.getSiblingDB(name) get the db at the same server as this one");
print(
"\tdb.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set");
print("\tdb.hostInfo() get details about the server's host");
print("\tdb.isMaster() check replica primary status");
print("\tdb.killOp(opid) kills the current operation in the db");
print("\tdb.listCommands() lists all the db commands");
print("\tdb.loadServerScripts() loads all the scripts in db.system.js");
print("\tdb.logout()");
print("\tdb.printCollectionStats()");
print("\tdb.printReplicationInfo()");
print("\tdb.printShardingStatus()");
print("\tdb.printSlaveReplicationInfo()");
print("\tdb.dropUser(username)");
print("\tdb.repairDatabase()");
print("\tdb.resetError()");
print(
"\tdb.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }");
print("\tdb.serverStatus()");
print("\tdb.setLogLevel(level,<component>)");
print("\tdb.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all");
print(
"\tdb.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db");
print(
"\tdb.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db");
print("\tdb.setVerboseShell(flag) display extra information in shell output");
print("\tdb.shutdownServer()");
print("\tdb.stats()");
print("\tdb.version() current version of the server");
return __magicNoPrint;
}
4.删除当前数据库:db.dropDatabase()
> use weigion
switched to db weigion
> db.test.insert({'key': 'value'})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
local 0.000GB
weigion 0.000GB
wgq 0.000GB
> db.dropDatabase()
{ "dropped" : "weigion", "ok" : 1 }
> show dbs
admin 0.000GB
local 0.000GB
wgq 0.000GB
5.查看当前使用的数据库:db或者db.getName()
> use wgq
switched to db wgq
> db
wgq
> db.getName()
wgq
6.显示当前db状态:db.stats()
> db.stats()
{
"db" : "wgq",
"collections" : 2,
"views" : 0,
"objects" : 6,
"avgObjSize" : 62.666666666666664,
"dataSize" : 376,
"storageSize" : 49152,
"numExtents" : 0,
"indexes" : 2,
"indexSize" : 49152,
"ok" : 1
}