Database Commands
All command documentation outlined below describes a command and
its available parameters and provides a document template or prototype
for each command. Some command documentation also includes the relevant
mongosh helpers.
To run a command against the current database, use db.runCommand():
db.runCommand( { <command> } )
To run an administrative command against the admin database, use db.adminCommand():
db.adminCommand( { <command> } )
Note
For details on specific commands, including syntax and examples, click on the specific command to go to its reference page.
Command Syntax
When you run a database command, you specify the command as a document
to db.runCommand(). The document's key is the command to run,
and the value is typically supplied as 1. The value does not affect
the output of the command for example:
db.runCommand( { hello: 1 } )
User Commands
Aggregation Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Performs aggregation tasks such as | Limited support for M0, M2, M5, and Flex clusters. | |
Counts the number of documents in a collection or a view. | Limited support for M0, M2, M5, and Flex clusters. | |
Displays the distinct values found for a specified key in a collection or a view. | Limited support for M0, M2, M5, and Flex clusters. | |
Performs map-reduce aggregation for large data sets. | No support for M0, M2, M5, and Flex clusters. |
Geospatial Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Removed in MongoDB 5.0. Performs a geospatial query that uses MongoDB's haystack index functionality. | No support for serverless instances. |
Query and Write Operation Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Deletes one or more documents. | Yes | |
Selects documents in a collection or a view. | Limited support for M0, M2, M5, and Flex clusters. | |
Returns and modifies a single document. | Yes | |
Returns batches of documents currently pointed to by the cursor. | Yes | |
Inserts one or more documents. | Yes | |
Removed in MongoDB 5.0. Resets the last error status. | No | |
Updates one or more documents. | Yes |
Query Plan Cache Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Removes cached query plan(s) for a collection. | No support for M0, M2, M5, and Flex clusters. | |
Clears index filter(s) for a collection. | No support for M0, M2, M5, and Flex clusters. | |
Lists the index filters for a collection. | No support for M0, M2, M5, and Flex clusters. | |
Sets an index filter for a collection. | No support for M0, M2, M5, and Flex clusters. |
Database Operations
Authentication Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Starts an authenticated session using a username and password. | No support for M0, M2, M5, and Flex clusters. | |
Terminates the current authenticated session. | Yes |
User Management Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Creates a new user. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Deletes all users associated with a database. | No support for M0, M2, M5, and Flex clusters. | |
Removes a single user. | No support for M10 clusters. | |
Grants a role and its privileges to a user. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Removes a role from a user. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Updates a user's data. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Returns information about the specified users. | Limited support for M0, M2, M5, and Flex clusters. |
Role Management Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Creates a role and specifies its privileges. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Deletes the user-defined role. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Deletes all user-defined roles from a database. | No support for M0, M2, M5, and Flex clusters. | |
Assigns privileges to a user-defined role. | No support for M0, M2, M5, and Flex clusters. | |
Specifies roles from which a user-defined role inherits privileges. | No support for M0, M2, M5, and Flex clusters. | |
Flushes the in-memory cache of user information, including credentials and roles. | No support for M0, M2, M5, and Flex clusters. | |
Removes the specified privileges from a user-defined role. | No support for M0, M2, M5, and Flex clusters. | |
Removes specified inherited roles from a user-defined role. | No support for M0, M2, M5, and Flex clusters. | |
Returns information for the specified role or roles. | Yes | |
Updates a user-defined role. | No support for Flex clusters, M0, M2, M5, and M10 clusters. |
Replication Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Writes a non-operational entry to the oplog. | No support for M0, M2, M5, and Flex clusters. | |
Internal command that applies oplog entries to the current data set. | No support for serverless instances. | |
Displays information about this member's role in the replica set, including whether it is the primary. | Yes | |
Forces the elected primary to abort sync (catch up) then complete the transition to primary. | Yes | |
Prevents the current member from seeking election as primary for a period of time. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Returns the replica set's configuration object. | No support for M0, M2, M5, and Flex clusters. | |
Returns a document that reports on the status of the replica set. | No support for M0, M2, M5, and Flex clusters. | |
Initializes a new replica set. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Enables or disables a maintenance mode, which puts a secondary node in a | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Applies a new configuration to an existing replica set. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Dynamically resizes the oplog for a replica set member. Available for WiredTiger storage engine only. | No support for M10 clusters. | |
No support for Flex clusters, M0, M2, M5, and M10 clusters. | ||
Explicitly override the default logic for selecting a member to replicate from. | No support for Flex clusters, M0, M2, M5, and M10 clusters. |
See also:
Replication for more information regarding replication.
Sharding Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Aborts a resharding operation. New in version 5.0. | Yes | |
Adds a shard to a sharded cluster. | No support for serverless instances. | |
No support for serverless instances. | ||
Calculates metrics for evaluating a shard key. New in version 7.0. | Yes | |
Returns information on whether the chunks of a sharded collection are balanced. | No support for serverless instances. | |
Starts a balancer thread. | No support for serverless instances. | |
Returns information on the balancer status. | No support for serverless instances. | |
Stops the balancer thread. | No support for serverless instances. | |
Performs a series of consistency checks on sharding metadata. New in version 7.0. | Yes | |
Clears the | No support for serverless instances. | |
Removes orphaned data with shard key values outside of the ranges of the chunks owned by a shard. | No support for M0, M2, and M5 clusters. | |
Cleans up a failed resharding operation. New in version 5.0. | Yes | |
Forces a resharding operation to block writes and complete. New in version 5.0. | Yes | |
Configures balancer settings on a sharded collection. New in version 5.3. | Yes | |
Configures query sampling for a collection. New in version 7.0. | Yes | |
Creates a database. | No support for serverless instances. | |
No support for serverless instances. | ||
Internal command that reports on the state of a sharded cluster. | No support for M0, M2, M5, and Flex clusters and serverless instances. | |
Verifies that a process is a | Yes | |
Returns a list of configured shards. | No support for serverless instances. | |
Internal command that migrates chunks between shards. | No support for serverless instances. | |
Reassigns the primary shard when removing a shard from a sharded cluster. | No support for serverless instances. | |
Moves ranges between shards. | Yes | |
Merges all mergeable chunks that a shard owns for a given collection. | No support for serverless instances. | |
Provides the ability to combine chunks on a single shard. | No support for serverless instances. | |
Refines a collection's shard key by adding a suffix to the existing key. | No support for M10 clusters and serverless instances. | |
Starts the process of removing a shard from a sharded cluster. | No support for M10 clusters and serverless instances. | |
No support for serverless instances. | ||
Initiates a resharding operation to change the shard key for a collection, changing the distribution of your data. New in version 5.0. | Yes | |
Prevents new automatic migrations on a collection. | No support for serverless instances. | |
Enables the sharding functionality for a collection, allowing the collection to be sharded. | No support for serverless instances. | |
Reports whether the | Yes | |
Creates a new chunk. | No support for serverless instances. | |
Removed in MongoDB 5.0. Internal command that affects connections between instances in a MongoDB deployment. | Yes | |
No support for serverless instances. |
See also:
Sharding for more information about MongoDB's sharding functionality.
Session Commands
Command | Description | Atlas Support |
|---|---|---|
Abort transaction. | Yes | |
Commit transaction. | Yes | |
Expire sessions before the sessions' timeout period. | Yes | |
Kill all sessions. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Kill all sessions that match the specified pattern | No support for M0, M2, M5, and Flex clusters. | |
Kill specified sessions. | No support for M0, M2, M5, and Flex clusters. | |
Refresh idle sessions. | Yes | |
Starts a new session. | Yes |
Administration Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Copies a non-capped collection as a new capped collection. | No support for M0, M2, M5, and Flex clusters and serverless instances. | |
Add options to a collection or modify a view definition. | Yes | |
Defragments a collection and rebuilds the indexes. | No support for M0, M2, M5, and Flex clusters. | |
Compacts documents specified in the state collections and deletes redundant documents. | Yes | |
Converts a non-capped collection to a capped collection. | No support for serverless instances. | |
Creates a collection or a view. | Yes | |
Builds one or more indexes for a collection. | Yes | |
Returns a document that contains information on in-progress operations for the database instance. | Yes | |
Removes the specified collection from the database. | Yes | |
Removes the current database. | Yes | |
Drops outgoing connections to the specified list of hosts. | Yes | |
Removes indexes from a collection. | Yes | |
Yes | ||
Flushes pending writes to the storage layer and locks the database to allow backups. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Unlocks one fsync lock. | No support for M0, M2, M5, and Flex clusters. | |
Retrieves the global default read and write concern options for the deployment. | Yes | |
Retrieves cluster parameter values from all nodes in a cluster. New in version 6.0. | Yes | |
Retrieves configuration options. | Limited support for M0, M2, M5, and Flex clusters. | |
Kills the specified cursors for a collection. | Yes | |
Terminates an operation as specified by the operation ID. | Limited support for M0, M2, M5, M10, and Flex clusters. | |
Returns a list of collections in the current database. | Yes | |
Returns a document that lists all databases and returns basic database statistics. | Yes | |
Lists all indexes for a collection. | Yes | |
Rotates the MongoDB logs to prevent a single file from taking too much space. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Rebuilds all indexes on a collection. | No support for M0, M2, M5, and Flex clusters. | |
Changes the name of an existing collection. | Yes | |
Performs online TLS certificate rotation. | Yes | |
Enables or disables features that persist data that are backwards-incompatible. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Changes the minimum number of data-bearing members (i.e commit quorum), including the primary, that must vote to commit an in-progress index build before the primary marks those indexes as ready. | Yes | |
Modifies configuration options for all nodes in a replica set or sharded cluster. New in version 6.0. | Yes | |
Modifies configuration options. | No support for Flex clusters, M0, M2, M5, and M10 clusters. | |
Blocks and unblocks writes to the entire cluster. | No | |
Sets the global default read and write concern options for the deployment. | Yes | |
No support for Flex clusters, M0, M2, M5, and M10 clusters. | ||
Checks that the stored metadata of a database or a collection is valid within a particular API version. New in version 5.0. | Yes |
Diagnostic Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
Displays statistics about the MongoDB build. | Yes | |
Reports storage utilization statics for a specified collection. | Yes | |
Reports statistics on the outgoing connections from this MongoDB instance to other MongoDB instances in the deployment. | Yes | |
Reports the authentication state for the current connection. | Yes | |
Returns the data size for a range of data. For internal use. | No support for M0, M2, M5, and Flex clusters. | |
Returns hash value a database and its collections. | No support for M0, M2, M5, and Flex clusters and serverless instances. | |
Reports storage utilization statistics for the specified database. | Limited support for M0, M2, M5, and Flex clusters. | |
Returns information on the execution of various operations. | Yes | |
Returns a document with the run-time arguments to the MongoDB instance and their parsed options. | Yes | |
Returns recent log messages. | Yes | |
Returns data that reflects the underlying host system. | No support for M0, M2, M5, and Flex clusters. | |
Lists all database commands provided by the current | Yes | |
Internal command that returns information on locks that are
currently being held or pending. Only available for
| No support for M0, M2, M5, and Flex clusters. | |
Internal command that tests intra-deployment connectivity. | Yes | |
Interface for the database profiler. | Yes | |
Returns a collection metrics on instance-wide resource utilization and status. | Yes | |
Removed in MongoDB 5.0. Use | No support for serverless instances. | |
Returns raw usage statistics for each database in the | No support for serverless instances. | |
Internal command that scans for a collection's data and indexes for correctness. | No support for M0, M2, M5, and Flex clusters and serverless instances. | |
Internal command that returns information on the current client. | Yes |
Auditing Commands
Name | Description | MongoDB Atlas Support |
|---|---|---|
No | ||
Posts a custom message to the audit log. | Yes | |
No |
Atlas Search Commands
New in version 7.0: (Also available starting in 6.0.7)
Name | Description |
|---|---|
Creates one or more Atlas Search indexes on a specified collection. | |
Deletes an existing Atlas Search index. | |
Updates an existing Atlas Search index. |