Methods
The following document lists the available methods in the MongoDB Shell. Click a method to see its documentation in the MongoDB Manual, including syntax and examples.
Important
Methods which reset the shell's connection to the server trigger a termination of all open server sessions. When a session ends, all in-progress operations are terminated as well, and the client must re-authenticate if authentication is enabled.
The following methods reset the shell's connection:
db.auth
Mongo.setReadPref
Mongo.setReadConcern
Administration Methods
Method | Description | ||||||
---|---|---|---|---|---|---|---|
Shuts down the current | |||||||
Flushes writes to disk and locks the database to prevent write operations and assist backup operations. | |||||||
Allows writes to continue on a database locked with Atlas Search indexes let you query data in Atlas Search. Atlas Search indexes enable performant text search queries by mapping search terms to the documents that contain those terms. Use the following methods to manage Atlas Search indexes. ImportantThe following methods can only be run on deployments hosted on db.collection.createSearchIndex() | Creates one or more Atlas Search indexes on a specified collection. | ||||||
Returns information about existing Atlas Search indexes on a specified collection. | |||||||
| |||||||
| |||||||
Creates a bulk operations builder used to construct a list of write
operations to perform in bulk for a single collection. To instantiate
the builder, use either the | |||||||
Sets the hint option that specifies the index to support the bulk operation. | |||||||
Adds a single document replacement operation to a bulk operations list. | |||||||
Adds a multi update operation to a bulk operations list. The method updates specific fields in existing documents. | |||||||
upsert option
to | |||||||
Returns an array of write operations executed through
| Returns a JSON document that contains the number of operations and
batches in the | Returns as a string a JSON document that contains the number of
operations and batches in the | Provides access to the
| Deprecated in | Returns a count of the number of documents in a collection or a
view. Wraps the | Returns an approximate count of the documents in a collection or a view. | |
Returns the size of the collection. Wraps the
| Returns an array of documents that have distinct values for the specified field. | ||||||
Deprecated. Use | Performs a query on a collection or a view and returns a cursor object. | ||||||
Finds a single document and replaces it. | |||||||
Returns an array of documents that describe the existing indexes on a collection. | |||||||
Returns information regarding the state of data in a sharded cluster. | |||||||
Changes the name of a collection. | |||||||
Reports on the state of a collection. Provides a wrapper around
the | Reports the total size used by the collection in bytes. Provides
a wrapper around the | Reports the total size used by the indexes on a collection.
Provides a wrapper around the | Reports the total size of a collection, including the size of all documents and all indexes on a collection. | ||||
Opens a change stream cursor on the collection. |
Connection Methods
Method | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JavaScript constructor to instantiate a database connection from
the The
Cursor Methods
Database Methods
In-Use Encryption MethodsNoteLimitations
| Decrypts the specified | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Encrypts the specified value using the specified | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Adds the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Adds a data encryption key to the key vault associated to the database connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deletes a data encryption key with the specified UUID from the key vault associated to the database connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Gets a data encryption key with the specified UUID. The data encryption key must exist in the key vault associated to the database connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns all data encryption keys stored in the key vault associated to the database connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Removes the specified |
Native Methods
Method | Description | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns the following JSON-formatted document that describes
your
| |||||||||||||||||||
| Returns a boolean indicating whether mongosh is running in interactive or script mode. | |||||||||||||||||||
| Loads and runs a JavaScript file in the shell. In The legacy | |||||||||||||||||||
| Print the specified text or variable.
| |||||||||||||||||||
| Exits the current shell session. | |||||||||||||||||||
| Suspends the | Returns an interface to access the query plan cache object and
associated | ||||||||||||||||||
Clears the cached query plans for the specified
| Lists the methods available to view and modify a collection’s query plan cache. | |||||||||||||||||||
Returns an array of plan cache entries for a collection. |
Replication Methods
Method | Description | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Adds a member to the replica set. You must connect to the primary of the replica set to run this method. | ||||||||||||
Makes the replica set member that | ||||||||||||
Returns the oplog of the replica set member that | ||||||||||||
Returns the oplog of the replica set member that | ||||||||||||
| Returns the status of the secondary members of the replica set. This is identical to the The following is an example output from the
| |||||||||||
| Returns the status of the secondary members of the replica set. This is identical to the The following is an example output from the
| |||||||||||
Returns the status of the replica set member that | ||||||||||||
Makes the primary of the replica set a secondary. You must be connected to the primary to run this method. | ||||||||||||
Resets the sync target to the replica set member specified
by hostname for the replica set member that |
Role Management Methods
Method | Description | |
---|---|---|
Returns information for the specified role. | ||
Returns information for all the user-defined roles in a database. | ||
Specifies roles from which a user-defined role inherits privileges. | ||
Updates the operation time. | ||
Returns the timestamp of the last acknowledged operation for the session. | ||
The options for a session in the shell. To access the
| Returns a document that describes the role of the
| |
Returns a document with information about the system running the MongoDB instance. | ||
Returns a document that displays the compilation parameters for
the | Returns a document with information about the runtime options used to start the MongoDB instance. | |
Returns a document that provides an overview of the database process. | ||
Returns a document that reports on the state of the current database. | ||
Aliases to | Associates a shard with a zone. Supports configuring zones in sharded clusters. | |
Disables automatic chunk merges for a namespace. New in version 7.0. | ||
Disables balancing on a single collection in a sharded database. Does not affect balancing of other collections in a sharded cluster. | ||
Activates the sharded collection balancer process if previously disabled using balancer is currently enabled. | ||
Returns data distribution information for sharded collections.
| ||
balancer process is currently migrating chunks. | ||
config shard.
If it does, | ||
Returns an array of documents describing the shards in a sharded cluster. | ||
Removes the association between a range of shard key values and a
| Removes the association between a tag and a shard. | |
Removes a range of shard key values to a shard tag created using
the | ||
Reports on the status of a sharded cluster. | ||
balancer. This operation does not wait for the balancer to complete any in progress operations, and may terminate ongoing operations. | ||
Associates a range of shard keys with a zone. Supports configuring zones in sharded clusters. |
Telemetry Methods
These methods configure whether mongosh
tracks anonymous telemetry
data. Telemetry is enabled by default.
For more information on what data mongosh
tracks with
telemetry, see enableTelemetry()
Enable telemetry for mongosh
.
Transaction Methods
Method | Description |
---|---|
multi-document transaction and rolls back any data changes made by the operations within the transaction. | |
multi-document transaction and ends the transaction. | |
multi-document transaction associated with the session. |
User Management Methods
Important
The passwordPrompt()
method is currently not
supported in mongosh
. As a result, when using the following
methods you must specify the password as a parameter:
Method | Description |
---|---|
Deletes all users associated with a database. | |
Updates a specified user's data. | |
Removes a role from a user. |