Write Scripts for the mongo Shell

  |   0 评论   |   71 浏览

参考链接

Opening New Connections

new Mongo()
new Mongo(<host>)
new Mongo(<host:port>)

conn = new Mongo();
db = conn.getDB("myDatabase");

db = connect("localhost:27020/myDatabase");

EXAMPLE

To print all items in a result cursor in mongo shell scripts, use the following idiom:

cursor = db.collection.find();
while ( cursor.hasNext() ) {
   printjson( cursor.next() );
}

Execute a JavaScript file

mongo localhost:27017/test myjsfile.js

load("myjstest.js")

The load() method accepts relative and absolute paths. If the current working directory of the mongo shell is /data/db, and the myjstest.js resides in the /data/db/scripts directory, then the following calls within the mongo shell would be equivalent:

load("scripts/myjstest.js")
load("/data/db/scripts/myjstest.js")


标题:Write Scripts for the mongo Shell
作者:SmiteLi
地址:https://smite.site/articles/2019/11/29/1575019588800.html

评论

发表评论