MongoDB Shell常用命令

Shell常用命令

標籤(空格分隔): MongoDB


連接到一個 mongod

 在命令行提示符下,輸入 mongo 命令來啓動 mongo 程序

C:\Users\bin>mongo
MongoDB shell version: 3.0.2
connecting to: test

 默認情況下 mongo 程序會自動嘗試去連接到本機localhost的 27017 端口。 若要連接到一個不同的服務器或者不同端口,你可以使用參數 –host 。
如:如果你想創建一個“myTest”的數據庫,先運行use myTest命令,之後就做一些操作(如:db.createCollection(‘user’)),這樣就可以創建一個名叫“myTest”的數據庫。

shell命令基本使用

 mongo 程序啓動時會默認選定 test 數據庫。

1. 打印出當前的數據庫名:

    > db
    test
    >

2. 列出所有數據庫:

  > show dbs
     admin  0.078GB
     db     0.078GB
     local  0.078GB
     test   0.078GB
  > 

3. 切換到一個新的數據庫 db:

  > use db
  switched to db db
  >

4. MongoDB自帶一個JavaScript Shell

  它是一個JavaScript解釋器,還是一個MongoDB的客戶端,可以通過JavaScript與啓動的數據庫實例進行交互(Shell中命令區分大小寫)。在Shell中,每當寫完一句完整的JS代碼,Shell就會將其結果返回。

5. 查看某個數據庫中所有的集合:show collections

> show collections
person
system.indexes
>

  如果該數據庫中有已經存在的集合,並該集合中插入了文檔,那麼使用該命令查看集合時會發現多了一個system.indexes的集合,它負責存儲索引,這是因爲在插入一個文檔時,如果沒有一個叫做“_id”的key,那麼會自動加入一個“_id”的key,系統默認會爲該key建立唯一索引,所以在增加一個system.indexes的集合。

6. 刪除數據庫中指定的集合:db.集合名.drop()。

> db.perso.drop()
true
>

7. 刪除當前數據庫:db.dropDatabase()。

8. shell中的help函數:

當進入到某個數據庫中,要如何知道可以使用哪些操作呢?此時就可以使用help函數,如下圖,就能夠列出數據庫級別有哪些用法了,當然除了數據庫級別的help,還有集合級別的help,使用方法爲:db.集合名.help()。在函數名稱後面不添加“()”還可以查看函數的源碼。

> db.help()
DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs comma
nd [ 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)
        db.createCollection(name, { size : ..., capped : ..., max : ... } )
        db.createUser(userDocument)
        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.getCollectionInfos()
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getLogComponents()
        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.getWriteConcern() - returns the write concern used for any operations
 on this db, inherited from server object if set
        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.dropUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, tu
rns it into { cmdObj : 1 }
        db.serverStatus()
        db.setLogLevel(level,<component>)
        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
        db.setWriteConcern( <write concern doc> ) - sets the write concern for w
rites to the db
        db.unsetWriteConcern( <write concern doc> ) - unsets the write concern f
or writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
>
//使用函數
> db.getName()
test
>
//查看函數源代碼
> db.getName
function (){
    return this._name;
}
>

8. Shell內置的JS引擎可以直接執行執行JS代碼

> function invokeEval(){
... return db.eval("return 123")
... }
> invokeEval()
123
>

9. connect()

  雖然Shell中提供的全局變量db指向當前連接的數據庫,但還可以用其它的變量來保存其它連接的數據庫,利用Shell中提供的connect()命令即可,

> db
test
> var udb = connect("127.0.0.1:27017/admin")
connecting to: 127.0.0.1:27017/admin
> udb
admin
> db
test
>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章