7.mongo command line run JavaScript script
You can manipulate the data and manage MongoDB by writing JavaScript scripts at the command line. For more information, see How to use the [Running. js files via a MONGO shell Instance on the Server] () in the MONGO script. Part.
This article describes how to connect MongoDB by writing JavaScript scripts on the MONGO command line.
① opening a new connection
In the MONGO command line or JavaScript file, you canMongo()create a db instance from a constructor.
New Mongo () New Mongo (
The following example is run with MongoDB on thedashidan.comdefault port, and the global database variable is set to MyDatabase, and a new connection instance can be obtained through the Getdb () method:
conn = new Mongo ();d B = Conn.getdb ("MyDatabase");
If the MongoDB connection instance has access control set, you can usedb.auth ()method to authenticate.
Plus, you can passconnect ()method links the MongoDB instance. The following is a connection to the MongoDB bindingdashidan.comnon-default port27020:
db = Connect ("Dashidan.com:27020/mydatabase");
② the difference between the command input and the MONGO script operation
When you write the MONGO script, you need to consider the following points:
To setdba global variable,getDB()you canconnect()set the database reference to another variable by means of a method or method.
Write operations in the MONGO shell with 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 command-line help in a JavaScript file (for example: using
, show DBS, etc.) Because JavaScript is not supported. The following are common command-line help with JavaScript tables:
Command-line Help Javascript
Show?dbs,?show?databases Db.admincommand (' listdatabases ')
Use? db = Db.getsiblingdb (")
Show?collections Db.getcollectionnames ()
Show?users Db.getusers ()
Show?roles Db.getroles ({showbuiltinroles:true})
Show?log? Db.admincommand ({' GetLog ': '})
Show?logs Db.admincommand ({' GetLog ': ' * '})
It cursor = Db.collection.find () if (Cursor.hasnext ()) {Cursor.next ();}
in command-line interaction mode MONGO will print out all the results. Using print ()orprintjson ()method to return the JSON format. Example: Print all the results in javascript:
cursor = Db.collection.find (); while (Cursor.hasnext ()) {Printjson (Cursor.next ());}
③ Writing scripts
In the system command line, usemongoJavaScript.
1.--eval Options
Use the--evaloptions to pass in the JavaScript script:
MONGO Test--eval "Printjson (Db.getcollectionnames ())"
This command connects the MONGO instance that is running ondashidan.com, bound Port 27017, anddb.getCollectionNames()returns the output of the method.
2. Executing JavaScript files
You can specify a.jsjavascript file as a suffix, MONGO can be executed directly. For example:
MONGO Dashidan.com:27017/test Myjsfile.js
This command is connected to run indashidan.com, bind the MONGO instance of Port 27017, and willmyjsfile.jsis returned.
mongo () constructor to execute MongoDB connection parameters. Refer to [Opening New Connections] () for more information.
In the MONGO command line you canload()run the file through.jsthe method, for example:
Load ("Myjstest.js")
This method loads and executes themyjstest.jsfile.
load()method accepts relative paths and absolute paths, and defaults to relative paths. Examples of using absolute paths:
Load ("Scripts/myjstest.js") Load ("/data/db/scripts/myjstest.js")
Path in ' Load () ' method does not have auto-find function
If neither the current path nor the absolute path can find the file, the script will not execute.
④ Reference Articles
Official documents
The latest content will be updated at the source station. Reprint please keep the original link: http://dashidan.com/article/mongodb/index.html
This article is from the "13402341" blog, please be sure to keep this source http://13412341.blog.51cto.com/13402341/1976415
7.mongo command line run JavaScript script