现在的位置: 首页 > 综合 > 正文

MongoDB基本管理命令

2013年04月21日 ⁄ 综合 ⁄ 共 3393字 ⁄ 字号 评论关闭

MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表;而每个集合中可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由一组列标识的实体的集合对应于关系数据库表中的行。下面通过熟悉MongoDB的基本管理命令,来了解MongoDB提供的DBMS的基本功能和行为。

MongoDB命令帮助系统

在安装MongoDB后,启动服务器进程(mongod),可以通过在客户端命令mongo实现对MongoDB的管理和监控。看一下MongoDB的命令帮助系统:

[plain] view
plain
copy

  1. root@dev2:~# mongo  
  2. MongoDB shell version: 1.8.3  
  3. connecting to: test  
  4. > help  
  5.         db.help()                    help on db methods  
  6.         db.mycoll.help()             help on collection methods  
  7.         rs.help()                    help on replica set methods  
  8.         help connect                 connecting to a db help  
  9.         help admin                   administrative help  
  10.         help misc                    misc things to know  
  11.         help mr                      mapreduce help  
  12.   
  13.         show dbs                     show database names  
  14.         show collections             show collections in current database  
  15.         show users                   show users in current database  
  16.         show profile                 show most recent system.profile entries with time >= 1ms  
  17.         use <db_name>                set current database  
  18.         db.foo.find()                list objects in collection foo  
  19.         db.foo.find( { a : 1 } )     list objects in foo where a == 1  
  20.         it                           result of the last line evaluated; use to further iterate  
  21.         DBQuery.shellBatchSize = x   set default number of items to display on shell  
  22.         exit                         quit the mongo shell  

这是MongoDB最顶层的命令列表,主要告诉我们管理数据库相关的一些抽象的范畴:数据库操作帮助、集合操作帮助、管理帮助。如果你想了解数据库操作更详细的帮助命令,可以直接使用db.help(),如下所示:

[plain] view
plain
copy

  1. > db.help()  
  2. DB methods:  
  3.         db.addUser(username, password[, readOnly=false])  
  4.         db.auth(username, password)  
  5.         db.cloneDatabase(fromhost)  
  6.         db.commandHelp(name) returns the help for the command  
  7.         db.copyDatabase(fromdb, todb, fromhost)  
  8.         db.createCollection(name, { size : ..., capped : ..., max : ... } )  
  9.         db.currentOp() displays the current operation in the db  
  10.         db.dropDatabase()  
  11.         db.eval(func, args) run code server-side  
  12.         db.getCollection(cname) same as db['cname'] or db.cname  
  13.         db.getCollectionNames()  
  14.         db.getLastError() - just returns the err msg string  
  15.         db.getLastErrorObj() - return full status object  
  16.         db.getMongo() get the server connection object  
  17.         db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair  
  18.         db.getName()  
  19.         db.getPrevError()  
  20.         db.getProfilingLevel() - deprecated  
  21.         db.getProfilingStatus() - returns if profiling is on and slow threshold   
  22.         db.getReplicationInfo()  
  23.         db.getSiblingDB(name) get the db at the same server as this one  
  24.         db.isMaster() check replica primary status  
  25.         db.killOp(opid) kills the current operation in the db  
  26.         db.listCommands() lists all the db commands  
  27.         db.printCollectionStats()  
  28.         db.printReplicationInfo()  
  29.         db.printSlaveReplicationInfo()  
  30.         db.printShardingStatus()  
  31.         db.removeUser(username)  
  32.         db.repairDatabase()  
  33.         db.resetError()  
  34.         db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }  
  35.         db.serverStatus()  
  36.         db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all  

抱歉!评论已关闭.