mongo
Shell¶On this page本页内容
Note
The following document pertains to the mongo
shell included in the MongoDB Server Download. For information on the new MongoDB Shell, mongosh
, refer to the mongosh Documentation.
To understand the differences between the two shells, see Comparison of the mongo Shell and mongosh.
You can write scripts for the mongo
shell in JavaScript that manipulate data in MongoDB or perform administrative operation.
This tutorial provides an introduction to writing JavaScript that uses the mongo
shell to access MongoDB.
From the mongo
shell or from a JavaScript file, you can instantiate database connections using the Mongo()
constructor:
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:
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 27020
and set the global db
variable:
See also参阅
mongo
¶Note
Starting in version 4.2, mongo
shell provides the method isInteractive()
that returns a boolean indicating whether the mongo
shell is running in interactive or script mode.
When writing scripts for the mongo
shell, consider the following:
db
global variable, use the getDB()
method or the connect()
method. You can assign the database reference to a variable other than db
.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.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 |
|
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.
Example
To print all items in a result cursor in mongo
shell scripts, use the following idiom:
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:
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.
You can specify a .js
file to the mongo
shell, and mongo
will execute the JavaScript directly. Consider the following example:
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:
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:
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.