mongodb shell之使用js(二)

來源:互聯網
上載者:User

標籤:http   io   使用   java   strong   for   資料   sp   問題   

mongodb shell之使用js(二)

mongodb shell不僅是個互動式shell,還能夠使用js指令碼進行訪問。

使用js指令碼進行互動的優點與缺點
(1)無需任何驅動或語言支援;
(2)方便cron或管理員定時任務;
(3)注意點:任然是資料格式的問題;

js指令碼一般會用來執行以下任務
(1)備份;
(2)調度map-reduce命令;
(3)離線報告,離線任務;
(4)管理員定時任務;

如何運行一個js指令碼
./mongo 127.0.0.1:3003/test–quiet test.js
說明:
(1)./mongo
mongo用戶端-_-;
(2)127.0.0.1:3003/test
伺服器的ip,port,以及需要串連的資料庫;
(3)–quiet
省略一些頭資訊
(4)test.js
待啟動並執行指令碼,內容是:
db.test.find().forEach(printjson);

也可以使用–eval參數
./mongo 127.0.0.1:3003/test –eval “db.test.find().forEach(printjson);”
–eval參數後面直接跟一個js語句。

其他的一些說明
(1)通常使用兩種方法列印擷取到的結果:
print() => 普通js
printjson() => 格式化json列印,列印出來的格式會漂亮許多
(2)迭代器(iterator command)在指令碼操作中將失效
(3)如何知道insert、delete、update等操作是否成功呢,請使用:
db.getLastErrorObj()
或者
db.getLastError()
(4)js shell的相關介紹見:
https://developer.mozilla.org/En/SpiderMonkey/Introduction_to_the_JavaScript_shell

 

http://www.habadog.com/2011/08/09/mongodb-shell-use-js/

mongodb shell之使用js(二)

聯繫我們

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