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 mongo shell 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.

User Commands

Aggregation Commands

Name Description
aggregate Performs aggregation tasks such as group using the aggregation framework.
count Counts the number of documents in a collection or a view.
distinct Displays the distinct values found for a specified key in a collection or a view.
group Deprecated. Groups documents in a collection by the specified key and performs simple aggregation.
mapReduce Performs map-reduce aggregation for large data sets.

Geospatial Commands

Name Description
geoNear Performs a geospatial query that returns the documents closest to a given point.
geoSearch Performs a geospatial query that uses MongoDB’s haystack index functionality.

Query and Write Operation Commands

Name Description
delete Deletes one or more documents.
eval Deprecated. Runs a JavaScript function on the database server.
find Selects documents in a collection or a view.
findAndModify Returns and modifies a single document.
getLastError Returns the success status of the last operation.
getMore Returns batches of documents currently pointed to by the cursor.
getPrevError Deprecated. Returns status document containing all errors since the last resetError command.
insert Inserts one or more documents.
parallelCollectionScan Lets applications use multiple parallel cursors when reading documents from a collection.
resetError Deprecated. Resets the last error status.
update Updates one or more documents.

Query Plan Cache Commands

Name Description
planCacheClear Removes cached query plan(s) for a collection.
planCacheClearFilters Clears index filter(s) for a collection.
planCacheListFilters Lists the index filters for a collection.
planCacheListPlans Displays the cached query plans for the specified query shape.
planCacheListQueryShapes Displays the query shapes for which cached query plans exist.
planCacheSetFilter Sets an index filter for a collection.

Database Operations

Authentication Commands

Name Description
authenticate Starts an authenticated session using a username and password.
authSchemaUpgrade Supports the upgrade process for user data between version 2.4 and 2.6.
copydbgetnonce This is an internal command to generate a one-time password for use with the copydb command.
getnonce This is an internal command to generate a one-time password for authentication.
logout Terminates the current authenticated session.

User Management Commands

Name Description
createUser Creates a new user.
dropAllUsersFromDatabase Deletes all users associated with a database.
dropUser Removes a single user.
grantRolesToUser Grants a role and its privileges to a user.
revokeRolesFromUser Removes a role from a user.
updateUser Updates a user’s data.
usersInfo Returns information about the specified users.

Role Management Commands

Name Description
createRole Creates a role and specifies its privileges.
dropRole Deletes the user-defined role.
dropAllRolesFromDatabase Deletes all user-defined roles from a database.
grantPrivilegesToRole Assigns privileges to a user-defined role.
grantRolesToRole Specifies roles from which a user-defined role inherits privileges.
invalidateUserCache Flushes the in-memory cache of user information, including credentials and roles.
revokePrivilegesFromRole Removes the specified privileges from a user-defined role.
revokeRolesFromRole Removes specified inherited roles from a user-defined role.
rolesInfo Returns information for the specified role or roles.
updateRole Updates a user-defined role.

Replication Commands

Name Description
applyOps Internal command that applies oplog entries to the current data set.
isMaster Displays information about this member’s role in the replica set, including whether it is the master.
replSetAbortPrimaryCatchUp Forces the elected primary to abort sync (catch up) then complete the transition to primary.
replSetFreeze Prevents the current member from seeking election as primary for a period of time.
replSetGetConfig Returns the replica set’s configuration object.
replSetGetStatus Returns a document that reports on the status of the replica set.
replSetInitiate Initializes a new replica set.
replSetMaintenance Enables or disables a maintenance mode, which puts a secondary node in a RECOVERING state.
replSetReconfig Applies a new configuration to an existing replica set.
replSetResizeOplog Dynamically resizes the oplog for a replica set member. Available for WiredTiger storage engine only.
replSetStepDown Forces the current primary to step down and become a secondary, forcing an election.
replSetSyncFrom Explicitly override the default logic for selecting a member to replicate from.
resync Forces a mongod to re-synchronize from the master. For master-slave replication only.

See also

Replication for more information regarding replication.

Sharding Commands

Name Description
addShard Adds a shard to a sharded cluster.
addShardToZone Associates a shard with a zone. Supports configuring zones in sharded clusters.