1. 程式人生 > >MongoDB Shell

MongoDB Shell

local while 結果 ini 輸出 splay data method conn

簡介

MongoDBShell = JavaScirpt解釋器 + MongoDB客戶端

技術分享圖片

MongoDB Shell 是 MongoDB 自帶的,使用google v8引擎實現的 JavaScript Shell,隨MongoDB—同發布, 它是MonoDB客戶端工具,可以在Shell中使用命令與MongoDB實例交互,對數據庫的管理操作(CURD、集群配置、狀態查看等)都可以通過MongoDBShell來完成。

基本功能

  • 執行JavaScript命令

    1 > "hello, world".replace("world", "MongoDB")
    hello, MongoDB
    2 > function factorial(n) {
    ...     if(n <= 1) return 1;
    ...     return factorial(n-1) * n;
    ... }
    3 > factorial(10)
    3628800
  • MongoDB客戶端一基本命令

    • 連接/切換數據庫 -- use dbname;
    • 數據插入 -- db.stu.insert(obj);
    • 數據查詢 -- db.stu.find(query);
    • 數據更新 -- db.stu.update(query,obj);
    • 數據刪除 -- db.stu.remove(query);

使用技巧

  • help查看幫助

    7 > help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce
    
        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
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        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
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
  • 執行腳本

    1. 直接運行 例如:mongo[--quiet]script.js

    2. 交互式運行 例如:load("script.js")

      準備一個js文檔testMongoShell.jg:

      db = connect("localhost:27017"); // 連接數據庫
      db = db.getSiblngDB("test");     // 選擇數據庫
      cursor = db.demo.find();         // 查詢集合
      
      while(cursor.hasNext())          // 叠代輸出結果
      {
          printjson( cursor
      .next() ) }

      直接運行

      # (anaconda3) whoami at iamwho in [~]
      $ mongo --quiet testMongoShell.js 
      { "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 }
      { "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 }
      { "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }
      
      # (anaconda3) whoami at iamwho in [~]
      $ mongo testMongoShell.js 
      MongoDB shell version v3.4.10
      connecting to: mongodb://127.0.0.1:27017
      MongoDB server version: 3.4.10
      connecting to: localhost:27017
      MongoDB server version: 3.4.10
      { "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 }
      { "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 }
      { "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }

      交互式運行

      1 > load("testMongoShell.js")
      connecting to: localhost:27017
      MongoDB server version: 3.4.10
      { "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 }
      { "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 }
      { "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }
      true
  • 執行命令行程序

    例如:run("ls")

    2 > run("ls")
    2018-02-05T23:15:05.650+0800 I - [thread1] shell: started program (sh5361):  /bin/ls
    sh5361| 1.json
    sh5361| AnacondaProjects
    sh5361| avimrc
    3 > run("cd avimrc")
    2018-02-05T23:15:27.155+0800 I - [thread1] shell: started program (sh5366):  cd avimrc
    sh5366| Unable to start program cd avimrc: No such file or directory
    255

    似乎也只能運行內置的shell命令,但是也有著很多的限制.

  • .mongorc.js 文件

    mongo啟動配置文件

    • 文件分為Global和Local,前者在/etc/.mongorc.js,後者在~下面
    • 可以配置命令提示符,具體參考這裏
  • 編輯復合變量EDITOR

    這個我忽略了,直接在shell配置文件中export一個EDITOR不就行了嗎

    在shell中使用edit + fileName or doc;即可

  • 更多命令可參考

    這裏

MongoDB Shell