MongoDB - The mongo Shell, Write Scripts for the mongo Shell

來源:互聯網
上載者:User

標籤:man   ssi   onchange   returns   control   following   ror   scripts   lua   

You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform administrative operation. For more information about the mongo shell, see the Running .js files via a mongo shell Instance on the Server section for more information about using these mongo script.

This tutorial provides an introduction to writing JavaScript that uses the mongo shell to access MongoDB.

 

Opening New Connections

From the mongo shell or from a JavaScript file, you can instantiate database connections using the Mongo()constructor:

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

Consider the following example that instantiates a new connection to the MongoDB instance running on localhost on the default port and sets the global db variable to myDatabase using the getDB() method:

> conn = new Mongo();2016-12-01T00:09:03.664+0800 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:29118 #8 (8 connections now open)connection to 127.0.0.1> db = conn.getDB("myDatabase");myDatabase

If connecting to a MongoDB instance that enforces access control, you can use the db.auth() method to authenticate.

Additionally, you can use the connect() method to connect to the MongoDB instance. The following example connects to the MongoDB instance that is running on localhost with the non-default port 27020and set the global db variable:

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

SEE ALSO: mongo Shell Methods

 

Differences Between Interactive and Scripted mongo

When writing scripts for the mongo shell, consider the following:

  • To set the db global variable, use the getDB() method or the connect() method. You can assign the database reference to a variable other than db.

  • Write operations in the mongo shell use a write concern of { w: 1 } by default. If performing bulk operations, use the Bulk() methods. See Write Method Acknowledgements for more information.

    Changed in version 2.6: Before MongoDB 2.6, call db.getLastError() explicitly to wait for the result of write operations.

  • You cannot use any shell helper (e.g. use <dbname>, show dbs, etc.) inside the JavaScript file because they are not valid JavaScript.

    The following table maps the most common mongo shell helpers to their JavaScript equivalents.

    Shell Helpers JavaScript Equivalents
     show dbs, show databases   db.adminCommand(‘listDatabases‘)
     use <db>  db = db.getSiblingDB(‘<db>‘)
     show collections  db.getCollectionNames()
     show users  db.getUsers()
     show roles  db.getRoles({showBuiltinRoles: true})
     show log <logname>  db.adminCommand({ ‘getLog‘ : ‘<logname>‘ }) 
     show logs  db.adminCommand({ ‘getLog‘ : ‘*‘ })
     it

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

  • In interactive mode, mongo prints the results of operations including the content of all cursors. In scripts, either use the JavaScript print() function or the mongo specific printjson() function which returns formatted JSON.

    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() );}

 

Scripting

From the system prompt, use mongo to evaluate JavaScript.

--eval option

Use the --eval option to mongo to pass the shell a JavaScript fragment, as in the following:

mongo test --eval "printjson(db.getCollectionNames())"

This returns the output of db.getCollectionNames() using the mongo shell connected to the mongod or mongos instance running on port 27017 on the localhost interface.

Execute a JavaScript file

You can specify a .js file to the mongo shell, and mongo will execute the JavaScript directly. Consider the following example:

mongo localhost:27017/test myjsfile.js

This operation executes the myjsfile.js script in a mongo shell that connects to the test database on the mongod instance accessible via the localhost interface on port 27017.

Alternately, you can specify the mongodb connection parameters inside of the javascript file using the Mongo() constructor. See Opening New Connections for more information.

You can execute a .js file from within the mongo shell, using the load() function, as in the following:

load("myjstest.js")

This function loads and executes the myjstest.js file.

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")

NOTE: There is no search path for the load() function. If the desired script is not in the current working directory or the full specified path, mongo will not be able to access the file.

 

MongoDB - The mongo Shell, Write Scripts for the mongo Shell

相關文章

聯繫我們

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