Database Commands (original) (raw)

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 relevantmongosh 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 a specific command, including syntax and examples, click on the link to the command's 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
aggregate Performs aggregation tasks such as $group using an aggregation pipeline. Limited support for M0, M2, M5, and Flex clusters.
count Counts the number of documents in a collection or a view. Limited support for M0, M2, M5, and Flex clusters.
distinct Displays the distinct values found for a specified key in a collection or a view. Limited support for M0, M2, M5, and Flex clusters.
mapReduce Performs map-reduce aggregation for large data sets. No support for M0, M2, M5, and Flex clusters.
Name Description MongoDB Atlas Support
geoSearch 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
bulkWrite Performs many insert, update, and delete operations on multiple collections in one request.New in version 8.0. Yes
delete Deletes one or more documents. Yes
find Selects documents in a collection or a view. Limited support for M0, M2, M5, and Flex clusters.
findAndModify Returns and modifies a single document. Yes
getMore Returns batches of documents currently pointed to by the cursor. Yes
insert Inserts one or more documents. Yes
resetError Removed in MongoDB 5.0. Resets the last error status. No
update Updates one or more documents. Yes
Name Description MongoDB Atlas Support
planCacheClear Removes cached query plan(s) for a collection. No support for M0, M2, M5, and Flex clusters.
planCacheClearFilters Clears index filter(s) for a collection. No support for M0, M2, M5, and Flex clusters.
planCacheListFilters Lists the index filters for a collection. No support for M0, M2, M5, and Flex clusters.
planCacheSetFilter Sets an index filter for a collection. No support for M0, M2, M5, and Flex clusters.
Name Description MongoDB Atlas Support
createUser Creates a new user. No support for Flex clusters, M0, M2, M5,and M10 clusters.
dropAllUsersFromDatabase Deletes all users associated with a database. No support for M0, M2, M5, and Flex clusters.
dropUser Removes a single user. No support for M10 clusters.
grantRolesToUser Grants a role and its privileges to a user. No support for Flex clusters, M0, M2, M5,and M10 clusters.
revokeRolesFromUser Removes a role from a user. No support for Flex clusters, M0, M2, M5,and M10 clusters.
updateUser Updates a user's data. No support for Flex clusters, M0, M2, M5,and M10 clusters.
usersInfo Returns information about the specified users. Limited support for M0, M2, M5, and Flex clusters.
Name Description MongoDB Atlas Support
createRole Creates a role and specifies its privileges. No support for Flex clusters, M0, M2, M5,and M10 clusters.
dropRole Deletes the user-defined role. No support for Flex clusters, M0, M2, M5,and M10 clusters.
dropAllRolesFromDatabase Deletes all user-defined roles from a database. No support for M0, M2, M5, and Flex clusters.
grantPrivilegesToRole Assigns privileges to a user-defined role. No support for M0, M2, M5, and Flex clusters.
grantRolesToRole Specifies roles from which a user-defined role inherits privileges. No support for M0, M2, M5, and Flex clusters.
invalidateUserCache Flushes the in-memory cache of user information, including credentials and roles. No support for M0, M2, M5, and Flex clusters.
revokePrivilegesFromRole Removes the specified privileges from a user-defined role. No support for M0, M2, M5, and Flex clusters.
revokeRolesFromRole Removes specified inherited roles from a user-defined role. No support for M0, M2, M5, and Flex clusters.
rolesInfo Returns information for the specified role or roles. Yes
updateRole Updates a user-defined role. No support for Flex clusters, M0, M2, M5,and M10 clusters.
Name Description MongoDB Atlas Support
appendOplogNote Writes a non-operational entry to the oplog. No support for M0, M2, M5, and Flex clusters.
applyOps Internal command that applies oplog entries to the current data set. No support for serverless instances.
hello Displays information about this member's role in the replica set, including whether it is the primary. Yes
replSetAbortPrimaryCatchUp Forces the elected primary to abort sync (catch up) then complete the transition to primary. Yes
replSetFreeze 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.
replSetGetConfig Returns the replica set's configuration object. No support for M0, M2, M5, and Flex clusters.
replSetGetStatus Returns a document that reports on the status of the replica set. No support for M0, M2, M5, and Flex clusters.
replSetInitiate Initializes a new replica set. No support for Flex clusters, M0, M2, M5,and M10 clusters.
replSetMaintenance Enables or disables a maintenance mode, which puts a secondary node in a RECOVERING state. No support for Flex clusters, M0, M2, M5,and M10 clusters.
replSetReconfig Applies a new configuration to an existing replica set. No support for Flex clusters, M0, M2, M5,and M10 clusters.
replSetResizeOplog Dynamically resizes the oplog for a replica set member. Available for WiredTiger storage engine only. No support for M10 clusters.
replSetStepDown Forces the current primary to step down and become a secondary, forcing an election. No support for Flex clusters, M0, M2, M5,and M10 clusters.
replSetSyncFrom 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.

See also:

Sharding for more information about MongoDB's sharding functionality.

Command Description Atlas Support
abortTransaction Abort transaction. Yes
commitTransaction Commit transaction. Yes
endSessions Expire sessions before the sessions' timeout period. Yes
killAllSessions Kill all sessions. No support for Flex clusters, M0, M2, M5,and M10 clusters.
killAllSessionsByPattern Kill all sessions that match the specified pattern No support for M0, M2, M5, and Flex clusters.
killSessions Kill specified sessions. No support for M0, M2, M5, and Flex clusters.
refreshSessions Refresh idle sessions. Yes
startSession Starts a new session. Yes
Name Description MongoDB Atlas Support
cloneCollectionAsCapped Copies a non-capped collection as a new capped collection. No support for M0, M2, M5, and Flex clustersand serverless instances.
collMod Add options to a collection or modify a view definition. Yes
compact Defragments a collection and rebuilds the indexes. No support for M0, M2, M5, and Flex clusters.
compactStructuredEncryptionData Compacts documents specified in the state collections and deletes redundant documents. Yes
convertToCapped Converts a non-capped collection to a capped collection. No support for serverless instances.
create Creates a collection or a view. Yes
createIndexes Builds one or more indexes for a collection. Yes
currentOp Returns a document that contains information on in-progress operations for the database instance. Yes
drop Removes the specified collection from the database. Yes
dropDatabase Removes the current database. Yes
dropConnections Drops outgoing connections to the specified list of hosts. Yes
dropIndexes Removes indexes from a collection. Yes
filemd5 Returns the md5 hash for files stored using GridFS. Yes
fsync 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.
fsyncUnlock Unlocks one fsync lock. No support for M0, M2, M5, and Flex clusters.
getDefaultRWConcern Retrieves the global default read and write concern options for the deployment. Yes
getClusterParameter Retrieves cluster parameter values from all nodes in a cluster.New in version 6.0. Yes
getParameter Retrieves configuration options. Limited support for M0, M2, M5, and Flex clusters.
killCursors Kills the specified cursors for a collection. Yes
killOp Terminates an operation as specified by the operation ID. Limited support for M0, M2, M5, M10, andFlex clusters.
listCollections Returns a list of collections in the current database. Yes
listDatabases Returns a document that lists all databases and returns basic database statistics. Yes
listIndexes Lists all indexes for a collection. Yes
logRotate 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.
reIndex Rebuilds all indexes on a collection. No support for M0, M2, M5, and Flex clusters.
renameCollection Changes the name of an existing collection. Yes
rotateCertificates Performs online TLS certificate rotation. Yes
setFeatureCompatibilityVersion Enables or disables features that persist data that are backwards-incompatible. No support for Flex clusters, M0, M2, M5,and M10 clusters.
setIndexCommitQuorum 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
setClusterParameter Modifies configuration options for all nodes in a replica set or sharded cluster.New in version 6.0. Yes
setParameter Modifies configuration options. No support for Flex clusters, M0, M2, M5,and M10 clusters.
setDefaultRWConcern Sets the global default read and write concern options for the deployment. Yes
setUserWriteBlockMode Blocks and unblocks writes to the entire cluster. No
shutdown Shuts down the mongod or mongos process. No support for Flex clusters, M0, M2, M5,and M10 clusters.
validateDBMetadata 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
buildInfo Displays statistics about the MongoDB build. Yes
collStats Reports storage utilization statics for a specified collection. Yes
connPoolStats Reports statistics on the outgoing connections from this MongoDB instance to other MongoDB instances in the deployment. Yes
connectionStatus Reports the authentication state for the current connection. Yes
dataSize Returns the data size for a range of data. For internal use. No support for M0, M2, M5, and Flex clusters.
dbHash Returns hash value a database and its collections. No support for M0, M2, M5, and Flex clustersand serverless instances.
dbStats Reports storage utilization statistics for the specified database. Limited support for M0, M2, M5, and Flex clusters.
explain Returns information on the execution of various operations. Yes
getCmdLineOpts Returns a document with the run-time arguments to the MongoDB instance and their parsed options. Yes
getLog Returns recent log messages. Yes
hostInfo Returns data that reflects the underlying host system. No support for M0, M2, M5, and Flex clusters.
listCommands Lists all database commands provided by the current mongod instance. Yes
lockInfo Internal command that returns information on locks that are currently being held or pending. Only available formongod instances. No support for M0, M2, M5, and Flex clusters.
ping Internal command that tests intra-deployment connectivity. Yes
profile Interface for the database profiler. Yes
serverStatus Returns a collection metrics on instance-wide resource utilization and status. Yes
shardConnPoolStats Removed in MongoDB 5.0. Use connPoolStats instead. No support for serverless instances.
top Returns raw usage statistics for each database in the mongod instance. No support for serverless instances.
validate Internal command that scans for a collection's data and indexes for correctness. No support for M0, M2, M5, and Flex clustersand serverless instances.
whatsmyuri Internal command that returns information on the current client. Yes

New in version 7.0: (Also available starting in 6.0.7)