mongo Shell Methods

JavaScript in MongoDB

Although these methods use JavaScript, most interactions with MongoDB do not use JavaScript but use an idiomatic driver in the language of the interacting application.

Note

For details on specific methods, including syntax and examples, click on the specific method to go to its reference page.

Collection

Name Description
db.collection.aggregate() Provides access to the aggregation pipeline.
db.collection.bulkWrite() Provides bulk write operation functionality.
db.collection.copyTo() Deprecated. Wraps eval to copy data between collections in a single MongoDB instance.
db.collection.count() Wraps count to return a count of the number of documents in a collection or a view.
db.collection.createIndex() Builds an index on a collection.
db.collection.createIndexes() Builds one or more indexes on a collection.
db.collection.dataSize() Returns the size of the collection. Wraps the size field in the output of the collStats.
db.collection.deleteOne() Deletes a single document in a collection.
db.collection.deleteMany() Deletes multiple documents in a collection.
db.collection.distinct() Returns an array of documents that have distinct values for the specified field.
db.collection.drop() Removes the specified collection from the database.
db.collection.dropIndex() Removes a specified index on a collection.
db.collection.dropIndexes() Removes all indexes on a collection.
db.collection.ensureIndex() Deprecated. Use db.collection.createIndex().
db.collection.explain() Returns information on the query execution of various methods.
db.collection.find() Performs a query on a collection or a view and returns a cursor object.
db.collection.findAndModify() Atomically modifies and returns a single document.
db.collection.findOne() Performs a query and returns a single document.
db.collection.findOneAndDelete() Finds a single document and deletes it.
db.collection.findOneAndReplace() Finds a single document and replaces it.
db.collection.findOneAndUpdate() Finds a single document and updates it.
db.collection.getIndexes() Returns an array of documents that describe the existing indexes on a collection.
db.collection.getShardDistribution() For collections in sharded clusters, db.collection.getShardDistribution() reports data of chunk distribution.
db.collection.getShardVersion() Internal diagnostic method for sharded cluster.
db.collection.group() Deprecated. Provides simple data aggregation function. Groups documents in a collection by a key, and processes the results. Use aggregate() for more complex data aggregation.
db.collection.insert() Creates a new document in a collection.
db.collection.insertOne() Inserts a new document in a collection.
db.collection.insertMany() Inserts several new document in a collection.
db.collection.isCapped() Reports if a collection is a capped collection.
db.collection.latencyStats() Returns latency statistics for a collection.
db.collection.mapReduce() Performs map-reduce style data aggregation.
db.collection.reIndex() Rebuilds all existing indexes on a collection.
db.collection.remove() Deletes documents from a collection.
db.collection.renameCollection() Changes the name of a collection.
db.collection.replaceOne() Replaces a single document in a collection.
db.collection.save() Provides a wrapper around an insert() and update() to insert new documents.
db.collection.stats() Reports on the state of a collection. Provides a wrapper around the collStats.
db.collection.storageSize() Reports the total size used by the collection in bytes. Provides a wrapper around the storageSize field of the collStats output.
db.collection.totalIndexSize() Reports the total size used by the indexes on a collection. Provides a wrapper around the totalIndexSize field of the collStats output.
db.collection.totalSize() Reports the total size of a collection, including the size of all documents and all indexes on a collection.
db.collection.update() Modifies a document in a collection.
db.collection.updateOne() Modifies a single document in a collection.
db.collection.updateMany() Modifies multiple documents in a collection.
db.collection.watch() Establishes a Change Stream on a collection.
db.collection.validate() Performs diagnostic operations on a collection.

Cursor

Name Description
cursor.addOption() Adds special wire protocol flags that modify the behavior of the query.’
cursor.allowPartialResults() Allows 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.batchSize() Controls the number of documents MongoDB will return to the client in a single network message.
cursor.close() Close a cursor and free associated server resources.
cursor.isClosed() Returns true if the cursor is closed.
cursor.collation() Specifies the collation for the cursor returned by the db.collection.find().
cursor.comment() Attaches a comment to the query to allow for traceability in the logs and the system.profile collection.
cursor.count() Modifies the cursor to return the number of documents in the result set rather than the documents themselves.
cursor.explain() Reports on the query execution plan for a cursor.
cursor.forEach() Applies a JavaScript function for every document in a cursor.
cursor.hasNext() Returns true if the cursor has documents and can be iterated.
cursor.hint() Forces MongoDB to use a specific index for a query.
cursor.isExhausted() Returns true if the cursor is closed and there are no objects remaining in the batch.
cursor.itcount() Computes the total number of documents in the cursor client-side by fetching and iterating the result set.
cursor.limit() Constrains the size of a cursor’s result set.
cursor.map() Applies a function to each document in a cursor and collects the return values in an array.
cursor.max() Specifies an exclusive upper index bound for a cursor. For use with cursor.hint()
cursor.maxScan() Specifies the maximum number of items to scan; documents for collection scans, keys for index scans.
cursor.maxTimeMS() Specifies a cumulative time limit in milliseconds for processing operations on a cursor.
cursor.min() Specifies an inclusive lower index bound for a cursor. For use with cursor.hint()
cursor.next() Returns the next document in a cursor.
cursor.noCursorTimeout() Instructs the server to avoid closing a cursor automatically after a period of inactivity.
cursor.objsLeftInBatch() Returns the number of documents left in the current cursor batch.
cursor.pretty() Configures the cursor to display results in an easy-to-read format.
cursor.readConcern() Specifies a read concern for a find() operation.
cursor.readPref() Specifies a read preference to a cursor to control how the client directs queries to a replica set.
cursor.returnKey() Modifies the cursor to return index keys rather than the documents.
cursor.showRecordId() Adds an internal storage engine ID field to each document returned by the cursor.
cursor.size() Returns a count of the documents in the cursor after applying