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 |
---|---|
Performs aggregation tasks such as $group using an aggregation pipeline. | |
Counts the number of documents in a collection or a view. | |
Displays the distinct values found for a specified key in a collection or a view. | |
Performs map-reduce aggregation for large data sets. |
Geospatial Commands
Name | Description |
---|---|
Removed in MongoDB 5.0. Performs a geospatial query that uses
MongoDB's haystack index functionality. |
Query and Write Operation Commands
Name | Description |
---|---|
Deletes one or more documents. | |
Selects documents in a collection or a view. | |
Returns and modifies a single document. | |
Returns batches of documents currently pointed to by the cursor. | |
Inserts one or more documents. | |
Removed in MongoDB 5.0. Resets the last error status. | |
Updates one or more documents. |
Query Plan Cache Commands
Name | Description |
---|---|
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. |
Database Operations
Authentication Commands
Name | Description |
---|---|
Starts an authenticated session using a username and password. | |
Terminates the current authenticated session. |
User Management Commands
Name | Description |
---|---|
Creates a new user. | |
Deletes all users associated with a database. | |
Removes a single user. | |
Grants a role and its privileges to a user. | |
Removes a role from a user. | |
Updates a user's data. | |
Returns information about the specified users. |
Role Management Commands
Name | Description |
---|---|
Creates a role and specifies its privileges. | |
Deletes the user-defined role. | |
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. | |
Updates a user-defined role. |
Replication Commands
Name | Description |
---|---|
Internal command that applies oplog entries to the current data set. | |
Displays information about this member's role in the replica set, including whether it is the primary. | |
Forces the elected primary to abort sync (catch up) then complete the transition to primary. | |
Prevents the current member from seeking election as primary for a period of time. | |
Returns the replica set's configuration object. | |
Returns a document that reports on the status of the replica set. | |
Initializes a new replica set. | |
Enables or disables a maintenance mode, which puts a secondary node in a RECOVERING state. | |
Applies a new configuration to an existing replica set. | |
Dynamically resizes the oplog for a replica set member. Available for WiredTiger storage engine only. | |
Explicitly override the default logic for selecting a member to replicate from. |
Sharding Commands
Name | Description |
---|---|
Aborts a resharding operation. New in version 5.0. | |
Adds a shard to a sharded cluster. | |
Returns information on whether the chunks of a sharded collection are balanced. New in version 4.4. | |
Starts a balancer thread. | |
Returns information on the balancer status. | |
Stops the balancer thread. | |
Internal command that validates index on shard key. | |
Clears the jumbo flag for a chunk. | |
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. | |
Forces a resharding operation to block writes and complete. New in version 5.0. | |
Configures balancer settings on a sharded collection. New in version 5.3. | |
Creates a database. | |
Internal command that reports on the state of a sharded cluster. | |
Internal command that returns the config server version. | |
Verifies that a process is a mongos . | |
Returns a list of configured shards. | |
Deprecated internal command. See splitVector . | |
Internal command that migrates chunks between shards. | |
Reassigns the primary shard when removing a shard from a sharded cluster. | |
Moves ranges between shards. | |
Provides the ability to combine chunks on a single shard. | |
Refines a collection's shard key by adding a suffix to the existing key. New in version 4.4. | |
Starts the process of removing a shard from a sharded cluster. | |
Initiates a resharding operation to change the shard key for a collection, changing the distribution of your data. New in version 5.0. | |
Internal command to sets the config server version. | |
Enables the sharding functionality for a collection, allowing the collection to be sharded. | |
Reports whether the mongod is a member of a sharded cluster. | |
Creates a new chunk. | |
Internal command that determines split points. | |
Removed in MongoDB 5.0. Internal command that affects
connections between instances in a MongoDB deployment. | |
Session Commands
Command | Description |
---|---|
Abort transaction. New in version 4.0. | |
Commit transaction. New in version 4.0. | |
Expire sessions before the sessions' timeout period. | |
Kill all sessions. | |
Kill all sessions that match the specified pattern | |
Kill specified sessions. | |
Refresh idle sessions. | |
Starts a new session. |
Administration Commands
Name | Description |
---|---|
Copies a non-capped collection as a new capped collection. | |
Add options to a collection or modify a view definition. | |
Defragments a collection and rebuilds the indexes. | |
Compacts documents specified in the state collections and
deletes redundant documents. | |
Converts a non-capped collection to a capped collection. | |
Creates a collection or a view. | |
Builds one or more indexes for a collection. | |
Returns a document that contains information on in-progress operations for the database instance. | |
Removes the specified collection from the database. | |
Removes the current database. | |
Drops outgoing connections to the specified list of hosts. | |
Removes indexes from a collection. | |
Flushes pending writes to the storage layer and locks the database to allow backups. | |
Unlocks one fsync lock. | |
Retrieves the global default read and write concern options for the deployment. New in version 4.4. | |
Retrieves cluster parameter values from all nodes in a cluster. New in version 6.0. | |
Retrieves configuration options. | |
Kills the specified cursors for a collection. | |
Terminates an operation as specified by the operation ID. | |
Returns a list of collections in the current database. | |
Returns a document that lists all databases and returns basic database statistics. | |
Lists all indexes for a collection. | |
Rotates the MongoDB logs to prevent a single file from taking too much space. | |
Rebuilds all indexes on a collection. | |
Changes the name of an existing collection. | |
Performs online TLS certificate rotation. | |
Enables or disables features that persist data that are backwards-incompatible. | |
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. | |
Modifies configuration options for all nodes in a replica set or sharded cluster. New in version 6.0. | |
Modifies configuration options. | |
Sets the global default read and write concern options for the deployment. New in version 4.4. | |
Diagnostic Commands
Name | Description |
---|---|
Displays statistics about the MongoDB build. | |
Reports storage utilization statics for a specified collection. | |
Reports statistics on the outgoing connections from this MongoDB instance to other MongoDB instances in the deployment. | |
Reports the authentication state for the current connection. | |
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. | |
Internal command that converts an ObjectId to a string to support tests. | |
Returns information on the execution of various operations. | |
Reports on features available in the current MongoDB instance. | |
Returns a document with the run-time arguments to the MongoDB instance and their parsed options. | |
Returns recent log messages. | |
Returns data that reflects the underlying host system. | |
Internal command to support testing. | |
Lists all database commands provided by the current mongod instance. | |
Internal command that returns information on locks that are
currently being held or pending. Only available for
mongod instances. | |
Internal command that reports on intra-deployment connectivity. Only available for mongos instances. | |
Internal command that tests intra-deployment connectivity. | |
Interface for the database profiler. | |
Returns a collection metrics on instance-wide resource utilization and status. | |
Removed in MongoDB 5.0. Use connPoolStats instead. | |
Returns raw usage statistics for each database in the mongod instance. | |
Internal command that scans for a collection's data and indexes for correctness. | |
Internal command that returns information on the current client. |
Free Monitoring Commands
Name | Description |
---|---|
Enables/disables free monitoring during runtime. |
Auditing Commands
Name | Description |
---|---|
Posts a custom message to the audit log. |