MongoDB整理筆記の指定命令和指定檔案

來源:互聯網
上載者:User

標籤:

    MongoDB shell 不僅僅是一個互動shell,它也支援執行指定javascript 檔案,也支援執行指定的命令片斷。有了這個特性,就可以將MongoDB 與linux shell 完美結合,完成大部分的日常管理和維護工作。

    執行指定命令

    例如,需要查詢test 庫的t1 表中的記錄數有多少,常用方法如下:

[[email protected] bin]# ./mongo testMongoDB shell version: 1.8.1connecting to: test> db.t1.count()7>

    通過命令列eval參數直接執行語句:   

[[email protected] bin]# ./mongo test --eval "printjson(db.t1.count())"MongoDB shell version: 1.8.1connecting to: test7

    執行指定檔案

    如果涉及到很多的操作後,才能得到結果,那麼用eval 的方式來做的話是不可能完成的,那麼更靈活的執行指定檔案的方式就派上用場了。例如我們仍然要查看test 庫t1 表中的記錄數:

    t1_count.js 就是我們要執行的檔案,裡面的內容如下

[[email protected] bin]# cat t1_count.jsvar totalcount = db.t1.count();printjson(‘Total count of t1 is : ‘ + totalcount);printjson(‘-----------------------‘);

    下面我們將執行這個檔案

[[email protected] bin]# ./mongo t1_count.jsMongoDB shell version: 1.8.1connecting to: test"Total count of t1 is : 7""-----------------------"

    大家可以看到最終得到t1表的記錄數7,那麼一些不必要的說明性文字我們不希望出現,怎麼處理?

[[email protected] bin]# ./mongo --quiet t1_count.js"Total count of t1 is : 7""-----------------------"[[email protected] bin]#

    通過指定quiet 參數,即可以將一些登入資訊屏蔽掉,這樣可以讓結果更清晰。

MongoDB整理筆記の指定命令和指定檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.