Docs Menu
Docs Home
/
MongoDB Manual
/

Database Commands

On this page

  • Command Syntax
  • User Commands
  • Database Operations
  • Auditing Commands
  • Atlas Search 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.

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 } )
Name
Description
MongoDB Atlas Support
Performs aggregation tasks such as $group using an aggregation pipeline.
Limited support for M0, M2, and M5 clusters.
Counts the number of documents in a collection or a view.
Limited support for M0, M2, and M5 clusters.
Displays the distinct values found for a specified key in a collection or a view.
Limited support for M0, M2, and M5 clusters.
Performs map-reduce aggregation for large data sets.
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.
Name
Description
MongoDB Atlas Support
Deletes one or more documents.
Yes
Selects documents in a collection or a view.
Limited support for M0, M2, and M5 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
Name
Description
MongoDB Atlas Support
Removes cached query plan(s) for a collection.
Clears index filter(s) for a collection.
Lists the index filters for a collection.
Sets an index filter for a collection.
Name
Description
MongoDB Atlas Support
Starts an authenticated session using a username and password.
Terminates the current authenticated session.
Yes
Name
Description
MongoDB Atlas Support
Creates a new user.
No support for M0, M2, M5 and M10 clusters.
Deletes all users associated with a database.
Removes a single user.
No support for M10 clusters.
Grants a role and its privileges to a user.
No support for M0, M2, M5 and M10 clusters.
Removes a role from a user.
No support for M0, M2, M5 and M10 clusters.
Updates a user's data.
No support for M0, M2, M5 and M10 clusters.
Returns information about the specified users.
Limited support for M0, M2, and M5 clusters.
Name
Description
MongoDB Atlas Support
Creates a role and specifies its privileges.
No support for M0, M2, M5 and M10 clusters.
Deletes the user-defined role.
No support for M0, M2, M5 and M10 clusters.
Deletes all user-defined roles from a database.
Assigns privileges to a user-defined role.
Specifies roles from which a user-defined role inherits privileges.
Flushes the in-memory cache of user information, including credentials and roles.
Removes the specified privileges from a user-defined role.
Removes specified inherited roles from a user-defined role.
Returns information for the specified role or roles.
Yes
Updates a user-defined role.
No support for M0, M2, M5 and M10 clusters.
Name
Description
MongoDB Atlas Support
Writes a non-operational entry to the oplog.
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 M0, M2, M5 and M10 clusters.
Returns the replica set's configuration object.
Returns a document that reports on the status of the replica set.
Initializes a new replica set.
No support for M0, M2, M5 and M10 clusters.
Enables or disables a maintenance mode, which puts a secondary node in a RECOVERING state.
No support for M0, M2, M5 and M10 clusters.
Applies a new configuration to an existing replica set.
No support for 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.
Forces the current primary to step down and become a secondary, forcing an election.
No support for M0, M2, M5 and M10 clusters.
Explicitly override the default logic for selecting a member to replicate from.
No support for M0, M2, M5 and M10 clusters.

Tip

See also:

Replication for more information regarding replication.

Name
Description
MongoDB Atlas Support

Aborts a resharding operation.

New in version 5.0.

Yes
No support for serverless instances.
Associates a shard with a zone. Supports configuring zones in sharded clusters.
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 jumbo flag for a chunk.
No support for serverless instances.
Removes orphaned data with shard key values outside of the ranges of the chunks owned by a shard.

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.
Forces a mongod / mongos instance to update its cached routing metadata.
No support for serverless instances.
Internal command that reports on the state of a sharded cluster.
Verifies that a process is a mongos.
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.
Starts the process of removing a shard from a sharded cluster.
Removes the association between a shard and a zone. Supports configuring zones in sharded clusters.
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 mongod is a member of a sharded cluster.
Yes
Creates a new chunk.
No support for serverless instances.
Internal command that determines split points.
Yes
Removed in MongoDB 5.0. Internal command that affects connections between instances in a MongoDB deployment.
Yes
Adds or removes the association between a range of sharded data and a zone. Supports configuring zones in sharded clusters.
No support for serverless instances.

Tip

See also:

Sharding for more information about MongoDB's sharding functionality.

Command
Description
Atlas Support
Abort transaction.
Yes
Commit transaction.
Yes
Expire sessions before the sessions' timeout period.
Yes
Kill all sessions.
No support for M0, M2, M5 and M10 clusters.
Kill all sessions that match the specified pattern
Kill specified sessions.
Refresh idle sessions.
Yes
Starts a new session.
Yes
Name
Description
MongoDB Atlas Support
Copies a non-capped collection as a new capped collection.
Add options to a collection or modify a view definition.
Yes
Defragments a collection and rebuilds the indexes.
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
Returns the md5 hash for files stored using GridFS.
Yes
Flushes pending writes to the storage layer and locks the database to allow backups.
No support for M0, M2, M5 and M10 clusters.
Unlocks one fsync lock.
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, and M5 clusters.
Kills the specified cursors for a collection.
Yes
Terminates an operation as specified by the operation ID.
Limited support for M0, M2, M5 and M10 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 M0, M2, M5 and M10 clusters.
Rebuilds all indexes on a collection.
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 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 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
Shuts down the mongod or mongos process.
No support for 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
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.
Returns hash value a database and its collections.
Reports storage utilization statistics for the specified database.
Limited support for M0, M2, and M5 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.
Lists all database commands provided by the current mongod instance.
Yes
Internal command that returns information on locks that are currently being held or pending. Only available for mongod instances.
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 connPoolStats instead.
No support for serverless instances.
Returns raw usage statistics for each database in the mongod instance.
No support for serverless instances.
Internal command that scans for a collection's data and indexes for correctness.
Internal command that returns information on the current client.
Yes
Name
Description
MongoDB Atlas Support
Retrieves audit configurations from mongod and mongos instances.
No
Posts a custom message to the audit log.
Yes
Sets audit configurations for mongod and mongos instances at runtime.
No

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.

Back

Connection String Examples