Navigation

MongoDB Shell Methods

Beta

The MongoDB Shell (mongosh) is currently available as a Beta release. The product, its features, and the corresponding documentation may change during the Beta stage.

The following document lists the available methods in the MongoDB Shell. Click a method to see its documentation in the MongoDB Manual, including syntax and examples.

Collection Methods

NameDescription
db.collection.aggregateProvides access to the aggregation pipeline.
db.collection.bulkWriteProvides bulk write operation functionality.
db.collection.countReturns a count of the number of documents in a collection or a view.
db.collection.countDocumentsReturns a count of the number of documents in a collection or a view. Wraps the $group aggregation stage with a $sum expression.
db.collection.estimatedDocumentCountReturns an approximate count of the documents in a collection or a view.
db.collection.createIndexBuilds an index on a collection.
db.collection.createIndexesBuilds one or more indexes on a collection.
db.collection.dataSizeReturns the size of the collection. Wraps the collStats.size field in the output of the collStats.
db.collection.deleteOneDeletes a single document in a collection.
db.collection.deleteManyDeletes multiple documents in a collection.
db.collection.distinctReturns an array of documents that have distinct values for the specified field.
db.collection.dropRemoves the specified collection from the database.
db.collection.dropIndexRemoves a specified index on a collection.
db.collection.dropIndexesRemoves all indexes on a collection.
db.collection.ensureIndexDeprecated. Use db.collection.createIndex.
db.collection.explainReturns information on the query execution of various methods.
db.collection.findPerforms a query on a collection or a view and returns a cursor object.
db.collection.findAndModifyAtomically modifies and returns a single document.
db.collection.findOnePerforms a query and returns a single document.
db.collection.findOneAndDeleteFinds a single document and deletes it.
db.collection.findOneAndReplaceFinds a single document and replaces it.
db.collection.findOneAndUpdateFinds a single document and updates it.
db.collection.getIndexesReturns an array of documents that describe the existing indexes on a collection.
db.collection.insertCreates a new document in a collection.
db.collection.insertOneInserts a new document in a collection.
db.collection.insertManyInserts several new document in a collection.
db.collection.isCappedReports if a collection is a capped collection.
db.collection.reIndexRebuilds all existing indexes on a collection.
db.collection.removeDeletes documents from a collection.
db.collection.renameCollectionChanges the name of a collection.
db.collection.replaceOneReplaces a single document in a collection.
db.collection.saveProvides a wrapper around an db.collection.insert and db.collection.update to insert new documents.
db.collection.statsReports on the state of a collection. Provides a wrapper around the collStats.
db.collection.storageSizeReports the total size used by the collection in bytes. Provides a wrapper around the collStats.storageSize field of the collStats output.
db.collection.totalIndexSizeReports the total size used by the indexes on a collection. Provides a wrapper around the collStats.totalIndexSize field of the collStats output.
db.collection.totalSizeReports the total size of a collection, including the size of all documents and all indexes on a collection.
db.collection.updateModifies a document in a collection.
db.collection.updateOneModifies a single document in a collection.
db.collection.updateManyModifies multiple documents in a collection.

Cursor Methods

NameDescription
cursor.addOptionAdds special wire protocol flags that modify the behavior of the query.
cursor.allowPartialResultsAllows db.collection.find operations against a sharded collection to return partial results, rather than an error, if one or more queried shards are unavailable.
cursor.batchSizeControls the number of documents MongoDB will return to the client in a single network message.
cursor.closeClose a cursor and free associated server resources.
cursor.collationSpecifies the collation for the cursor returned by the db.collection.find.
cursor.commentAttaches a comment to the query to allow for traceability in the logs and the system.profile collection.
cursor.countModifies the cursor to return the number of documents in the result set rather than the documents themselves.
cursor.explainReports on the query execution plan for a cursor.
cursor.forEachApplies a JavaScript function for every document in a cursor.
cursor.hasNextReturns true if the cursor has documents and can be iterated.
cursor.hintForces MongoDB to use a specific index for a query.
cursor.isClosedReturns true if the cursor is closed.
cursor.isExhaustedReturns true if the cursor is closed and there are no objects remaining in the batch.
cursor.itcountComputes the total number of documents in the cursor client-side by fetching and iterating the result set.
cursor.limitConstrains the size of a cursor's result set.
cursor.mapApplies a function to each document in a cursor and collects the return values in an array.
cursor.maxSpecifies an exclusive upper index bound for a cursor. For use with cursor.hint
cursor.maxTimeMSSpecifies a cumulative time limit in milliseconds for processing operations on a cursor.
cursor.minSpecifies an inclusive lower index bound for a cursor. For use with cursor.hint
cursor.nextReturns the next document in a cursor.
cursor.noCursorTimeoutInstructs the server to avoid closing a cursor automatically after a period of inactivity.
cursor.readPrefSpecifies a read preference to a cursor to control how the client directs queries to a replica set.
cursor.returnKeyModifies the cursor to return index keys rather than the documents.
cursor.sizeReturns a count of the documents in the cursor after applying cursor.skip and cursor.limit methods.
cursor.skipReturns a cursor that begins returning results only after passing or skipping a number of documents.
cursor.sortReturns results ordered according to a sort specification.
cursor.tailableMarks the cursor as tailable. Only valid for cursors over capped collections.
cursor.toArrayReturns an array that contains all documents returned by the cursor.

Database Methods

NameDescription
db.adminCommandRuns a command against the admin database.
db.aggregateRuns admin/diagnostic pipeline which does not require an underlying collection.
db.dropDatabaseRemoves the current database.
db.getCollectionReturns a collection or view object. Used to access collections with names that are not valid in the mongo shell.
db.getCollectionInfosReturns collection information for all collections and views in the current database.
db.getCollectionNamesLists all collections and views in the current database.
db.getNameReturns the name of the current database.
db.getSiblingDBProvides access to the specified database.
db.logoutEnds an authenticated session.
db.runCommandRuns a database command.

User Management Methods

important

The passwordPrompt method is currently not supported in mongosh. As a result, when using the following methods you must specify the password as a parameter:

MethodDescription
db.authAuthenticates a user to a database.
db.changeUserPasswordChanges an existing user’s password.
db.createUserCreates a new user.
db.dropAllUsersDeletes all users associated with a database.
db.dropUserDeletes a single user.
db.getUserReturns information about the specified user.
db.getUsersReturns information about all users associated with a database.
db.updateUserUpdates a specified user's data.
db.grantRolesToUserGrants a role and its privileges to a user.
db.revokeRolesFromUserRemoves a role from a user.