mongo
Shell Help¶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.
In addition to the documentation in the MongoDB Manual, the mongo
shell provides some additional information in its “online” help system. This document provides an overview of accessing this help information.
To see the list of options and help for starting the mongo
shell, use the --help
option from the command line:
In the mongo
shell:
show dbs
command: [1]
show databases
is an alias for show dbs
.
db
object, call the db.help()
method:
db.<method name>
without the parenthesis (()
), as in the following example which will return the implementation of the method db.updateUser()
:
[1] | If the deployment runs with access control, the operation returns different values based on user privileges. See listDatabases Behavior for details. |
In the mongo
shell:
show collections
command:
See also参阅
db.<collection>
), use the db.<collection>.help()
method:
<collection>
can be the name of a collection that exists, although you may specify a collection that doesn’t exist.
db.<collection>.<method>
name without the parenthesis (()
), as in the following example which will return the implementation of the save()
method:
When you perform read operations with the find()
method in the mongo
shell, you can use various cursor methods to modify the find()
behavior and various JavaScript methods to handle the cursor returned from the find()
method.
db.collection.find().help()
command:
<collection>
can be the name of a collection that exists, although you may specify a collection that doesn’t exist.
db.<collection>.find().<method>
name without the parenthesis (()
), as in the following example which will return the implementation of the toArray()
method:
Some useful methods for handling cursors are:
hasNext()
which checks whether the cursor has more documents to return.next()
which returns the next document and advances the cursor position forward by one.forEach(<function>)
which iterates the whole cursor and applies the <function>
to each document returned by the cursor. The <function>
expects a single argument which corresponds to the document from each iteration.For examples on iterating a cursor and retrieving the documents from the cursor, see cursor handling. See also Cursor for all available cursor methods.