In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The 7.mongo command line runs the JavaScript script
The latest content will be updated at the origin server. To reprint, please keep the original link: http://dashidan.com/article/mongodb/index.html
You can process data and manage mongodb by writing JavaScript scripts on the command line. For more information, see how to use the [Running .js files via a mongo shell Instance on the Server] () section of the mongo script.
This article introduces how to connect to MongoDB. Mongo by writing a JavaScript script in the MongoDB command line.
① opens a new connection
In the mongo command line or in the JavaScript file, you can create a database instance through the Mongo () constructor.
New Mongo () new Mongo () new Mongo ()
The following example runs as MongoDB on the default port of dashidan.com and sets the global database variable to myDatabase. A new connection instance can be obtained through the getDB () method:
Conn = new Mongo (); db = conn.getDB ("myDatabase")
If the Mongodb connection instance has access control, you can authenticate it by using the db.auth () method.
In addition, you can link to the MongoDB instance through the connect () method. The following is an example of connecting to a MongoDB-bound dashidan.com non-default port 27020:
Db = connect ("dashidan.com:27020/myDatabase")
The difference between entering ② commands and operating through mongo scripts
When you write mongo scripts, you need to consider the following:
Set the global db variable, either through the getDB () method or the connect () method, you can set this database reference to other variables.
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 command line help in the JavaScript file, (for example: use
, show dbs, etc.) Because it is not supported in JavaScript. The following is a common command line help JavaScript comparison table:
Command line help JavaScriptshow?dbs,?show?databasesdb.adminCommand ('listDatabases') use?db = db.getSiblingDB ('') show?collectionsdb.getCollectionNames () show?usersdb.getUsers () show?rolesdb.getRoles ({showBuiltinRoles: true}) show?log?db.adminCommand ({'getLog':'}) show?logsdb.adminCommand ({'getLog':' *'}) itcursor = db.collection.find () if (cursor.hasNext ()) {cursor.next ();}
Mongo prints all the results in command-line interaction mode. Use the print () or printjson () method in the JavaScript script to return the JSON format. Example: print all the results in JavaScript:
Cursor = db.collection.find (); while (cursor.hasNext ()) {printjson (cursor.next ());
③ writes scripts
On the system command line, use mongo to use JavaScript.
1.--eval option
Use the-- eval option to pass in the JavaScript script:
Mongo test-eval "printjson (db.getCollectionNames ())"
This command connects to the mongo instance running on dashidan.com, binds port 27017, and returns the output of the db.getCollectionNames () method.
two。 Execute JavaScript file
You can specify a JavaScript file with the suffix .js directly, and mongo can execute it directly. For example:
Mongo dashidan.com:27017/test myjsfile.js
This command connects to the mongo instance running on dashidan.com, binds port 27017, and returns the running result of myjsfile.js.
In addition, you can execute mongodb connection parameters through the Mongo () constructor in the JavaScript file. Refer to [Opening New Connections] () for more information.
From the mongo command line, you can run the .js file through the load () method, for example:
Load ("myjstest.js")
This method loads and executes the myjstest.js file.
The load () method accepts relative and absolute paths, which defaults to relative paths. Examples of using absolute paths:
There is no automatic path lookup function in load ("scripts/myjstest.js") load ("/ data/db/scripts/myjstest.js") `load () `method.
If neither the current path nor the absolute path can find the file, the change script will not be executed.
④ reference articles
Official document
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.