Mongodb使用shell實現與javascript的動態互動

來源:互聯網
上載者:User

標籤:target   iss   命令   start   必須   key   eva   互動   ext   

關於利用mongodb的shell執行指令碼,這點在以前的文章中有點遺漏;現在在此篇部落格中做個補充;

一、在命令列中傳入指令檔

定義一個javasciprt檔案,名稱為:script1.js,內容如下: 

  1. print("I am albert shao in the script1.js")  

定義另一個javascript檔案,名稱為script2,內容如下:

  1. print("I like to read and study")  

在命令視窗運行得到如下結果:

備忘: 指令檔必須放到mongo的同目錄檔案下

如果希望使用指定的主機和連接埠的mongodb上運行指令碼,需要先指定地址然後在跟上指令檔的名稱;

mongo --quiet server-1 :3000/foo script1.js script2.js

 

二、互動執行js指令碼 三、建立.mongorc.js檔案

 

如果某些指令碼會被頻繁載入,可以將它們添加到mongor.js檔案中。這個檔案會在啟動shell時候自動運行;

例如我們希望啟動shell顯示一句歡迎語句。我們在使用者的主目錄建立一個名為【.mongorc.js】檔案,向其中添加如下代碼:

  1. var compliment = ["like albert","attractive","intellgient"];  
  2. var index = Math.floor(Math.random()*3);  
  3.   
  4. print("Hello, you‘re looking particularly " + compliment[index] + "  today!");  

預想可能會直接自動載入,檔案目錄:

 

運行會出現:

 

如果不在administer目錄 則需要指定,如下一樣:

 另外,mongo --eval 運行一段指令碼

 不進入互動模式,直接在 OS 的命令列下運行一段mongodb指令碼。 mongo 127.0.0.1:27017/test --eval "printjson(db.users.findOne())" 參考資料:http://blog.csdn.net/sxb0841901116/article/details/41260095

Mongodb使用shell實現與javascript的動態互動

相關文章

聯繫我們

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