標籤:
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整理筆記の指定命令和指定檔案