MongoDB基本管理命令

來源:互聯網
上載者:User

MongoDB是一個NoSQL資料庫系統:一個資料庫可以包含多個集合(Collection),每個集合對應於關聯式資料庫中的表;而每個集合中可以儲存一組由列標識的記錄,列是可以自由定義的,非常靈活,由一組列標識的實體的集合對應於關聯式資料庫表中的行。下面通過熟悉MongoDB的基本管理命令,來瞭解MongoDB提供的DBMS的準系統和行為。

MongoDB命令協助系統

在安裝MongoDB後,啟動伺服器處理序(mongod),可以通過在用戶端命令mongo實現對MongoDB的管理和監控。看一下MongoDB的命令協助系統:

  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(),如下所示:
  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  
  37.         db.shutdownServer()  
  38.         db.stats()  
  39.         db.version() current version of the server  
  40.         db.getMongo().setSlaveOk() allow queries on a replication slave server  
對資料庫進行管理和操作的基本命令,可以從上面擷取到。如果想要得到更多,而且每個命令的詳細用法,可以使用上面列出的db.listCommands()查詢。

另一個比較基礎的是對指定資料庫的集合進行操作、管理和監控,可以通過查詢db.mycoll.help()擷取到:

  1. > db.mycoll.help()  
  2. DBCollection help  
  3.         db.mycoll.find().help() - show DBCursor help  
  4.         db.mycoll.count()  
  5.         db.mycoll.dataSize()  
  6.         db.mycoll.distinct( key ) - eg. db.mycoll.distinct( 'x' )  
  7.         db.mycoll.drop() drop the collection  
  8.         db.mycoll.dropIndex(name)  
  9.         db.mycoll.dropIndexes()  
  10.         db.mycoll.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups  
  11.         db.mycoll.reIndex()  
  12.         db.mycoll.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.  
  13.                                                       e.g. db.mycoll.find( {x:77} , {name:1, x:1} )  
  14.         db.mycoll.find(...).count()  
  15.         db.mycoll.find(...).limit(n)  
  16.         db.mycoll.find(...).skip(n)  
  17.         db.mycoll.find(...).sort(...)  
  18.         db.mycoll.findOne([query])  
  19.         db.mycoll.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )  
  20.         db.mycoll.getDB() get DB object associated with collection  
  21.         db.mycoll.getIndexes()  
  22.         db.mycoll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )  
  23.         db.mycoll.mapReduce( mapFunction , reduceFunction , <optional params> )  
  24.         db.mycoll.remove(query)  
  25.         db.mycoll.renameCollection( newName , <dropTarget> ) renames the collection.  
  26.         db.mycoll.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name  
  27.         db.mycoll.save(obj)  
  28.         db.mycoll.stats()  
  29.         db.mycoll.storageSize() - includes free space allocated to this collection  
  30.         db.mycoll.totalIndexSize() - size in bytes of all the indexes  
  31.         db.mycoll.totalSize() - storage allocated for all data and indexes  
  32.         db.mycoll.update(query, object[, upsert_bool, multi_bool])  
  33.         db.mycoll.validate() - SLOW  
  34.         db.mycoll.getShardVersion() - only for use with sharding  

有關資料庫和集合管理的相關命令,是最基礎和最常用的,如集合查詢、索引操作等。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.