Database Commands
On this page
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 relevantmongo
shell helpers.
To run a command against the current database, usedb.runCommand()
:
db
.
runCommand
(
{
<
command
>
}
)
To run an administrative command against theadmin
database, usedb.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 |
Performsaggregation taskssuch 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 |
Performsmap-reduceaggregation 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’shaystack indexfunctionality. |
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 |
Returns status document containing all errors since the lastresetError command. |
insert |
Inserts one or more documents. |
parallelCollectionScan |
Lets applications use multiple parallel cursors when reading documents from a collection. |
resetError |
Resets the last error status. |
update |
Updates one or more documents. |
Query Plan Cache Commands
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 thecopydb command. |
getnonce |
This is an internal command to generate a one-time password for authentication. |
logout |
Terminates the current authenticated session. |
User Management Commands
Role Management Commands
Replication Commands
SEE ALSO
Replicationfor more information regarding replication.
Sharding Commands
SEE ALSO
Shardingfor more information about MongoDB’s sharding functionality.
Instance Administration Commands
Name |
Description |
clean |
Internal namespace administration command. |
clone |
Copies a database from a remote host to the current host. |
cloneCollection |
Copies a collection from a remote host to the current host. |
cloneCollectionAsCapped |
Copies a non-capped collection as a newcapped collection. |
collMod |
Add options to a collection or modify a view definition. |
compact |
Defragments a collection and rebuilds the indexes. |
connPoolSync |
Internal command to flush connection pool. |
connectionStatus |
Reports the authentication state for the current connection. |
convertToCapped |
Converts a non-capped collection to a capped collection. |
copydb |
Copies a database from a remote host to the current host. |
create |
Creates a collection or a view. |
createIndexes |
Builds one or more indexes for a collection. |
currentOp |
Returns a document that contains information on in-progress operations for the database instance. |
drop |
Removes the specified collection from the database. |
dropDatabase |
Removes the current database. |
dropIndexes |
Removes indexes from a collection. |
filemd5 |
Returns themd5hash for files stored usingGridFS. |
fsync |
Flushes pending writes to the storage layer and locks the database to allow backups. |
getParameter |
Retrieves configuration options. |
killCursors |
Kills the specified cursors for a collection. |
killOp |
Terminates an operation as specified by the operation ID. |
listCollections |
Returns a list of collections in the current database. |
listIndexes |
Lists all indexes for a collection. |
logRotate |
Rotates the MongoDB logs to prevent a single file from taking too much space. |
reIndex |
Rebuilds all indexes on a collection. |
renameCollection |
Changes the name of an existing collection. |
repairCursor |
Returns a cursor that iterates over all valid documents in a collection. |
repairDatabase |
Rebuilds the database and indexes by discarding invalid or corrupt data. |
setFeatureCompatibilityVersion |
Enables or disables MongoDB 3.4 features that persist data that are backwards-incompatible with MongoDB 3.2. |
setParameter |
Modifies configuration options. |
shutdown |
Shuts down themongod ormongos process. |
touch |
Loads documents and indexes from data storage to memory. |
Diagnostic Commands
Name |
Description |
availableQueryOptions |
Internal command that reports on the capabilities of the current MongoDB instance. |
buildInfo |
Displays statistics about the MongoDB build. |
collStats |
Reports storage utilization statics for a specified collection. |
connPoolStats |
Reports statistics on the outgoing connections from this MongoDB instance to other MongoDB instances in the deployment. |
cursorInfo |
Removed in MongoDB 3.2. Replaced withmetrics.cursor . |
dataSize |
Returns the data size for a range of data. For internal use. |
dbHash |
Returns hash value a database and its collections. |
dbStats |
Reports storage utilization statistics for the specified database. |
diagLogging |
Provides a diagnostic logging. For internal use. |
driverOIDTest |
Internal command that converts an ObjectId to a string to support tests. |
explain |
Returns information on the execution of various operations. |
features |
Reports on features available in the current MongoDB instance. |
getCmdLineOpts |
Returns a document with the run-time arguments to the MongoDB instance and their parsed options. |
getLog |
Returns recent log messages. |
hostInfo |
Returns data that reflects the underlying host system. |
isSelf |
Internal command to support testing. |
listCommands |
Lists all database commands provided by the currentmongod instance. |
listDatabases |
Returns a document that lists all databases and returns basic database statistics. |
netstat |
Internal command that reports on intra-deployment connectivity. Only available formongos instances. |
ping |
Internal command that tests intra-deployment connectivity. |
profile |
Interface for thedatabase profiler. |
serverStatus |
Returns a collection metrics on instance-wide resource utilization and status. |
shardConnPoolStats |
Reports statistics on amongos ’s connection pool for client operations against shards. |
top |
Returns raw usage statistics for each database in themongod instance. |
validate |
Internal command that scans for a collection’s data and indexes for correctness. |
whatsmyuri |
Internal command that returns information on the current client. |
Auditing Commands