Collection commands

Command

Description

Output

db.collection.aggregate( [BSON, ...] *pipelineStages* )

Runs an aggregation pipeline, which is capable of advanced aggregation operations such as sorts, distincts, groupings. For example: db.identities.aggregate( [ { $sort : { superhero : -1 } } ] ); See aggregation stages for details.

An output row for each BSON document that results from the aggregate command.

db.collection.countDocuments( )

Counts the documents in a collection. For example: db.identities.countDocuments(); See db.collection.countDocuments for details on a similar shell command.

An output row with the result field containing the count of all documents in the collection.

db.collection.countDocuments( BSON query )

Counts the documents in a collection. For example: db.identities.countDocuments( { superhero : { $gt : "S" } } ); See db.collection.countDocuments for details on query formation from a similar shell command.

An output row with the result field containing the count of all documents that matched the query in the collection.

db.collection.countDocuments( BSON *query*, BSON *options* )

Counts the documents in a collection. Option: { limit: INTEGER, skip: INTEGER, maxTimeMS: LONG }

For example: db.identities.countDocuments( { superhero : { $gt : "S" } }, { limit : 1 } );

See db.collection.countDocuments for details on query formation from a similar shell command.

An output row with the result field containing the count of all documents that matched the query in the collection while respecting the options provided.

db.collection.deleteMany( BSON *filter* )

Deletes one or more documents in the collection matching the filter. For example: db.identities.deleteMany( { superhero : { $gt : "Z" } } ); See db.collection.deleteMany for filter creation details from a similar shell command.

An output row with the result field containing a BSON document with the deletedCount and a Boolean value indicating the command was acknowledged.

db.collection.deleteOne( BSON *filter* )

Deletes at most one document in the collection matching the filter. For example: db.identities.deleteOne( { superhero : { $gt : "D" } } ); See db.collection.deleteOne for filter creation details from a similar shell command.

An output row with the result field containing a BSON document with the deletedCount and a Boolean indicating the command was acknowledged.

db.collection.drop( )

Drops a collection. For example: db.createCollection( "aliases" ); db.aliases.drop();

The String = "true" when the collection is dropped/removed. .

db.collection.find( )

Finds the documents in a collection. For example: db.identities.find(); See db.collection.find for details on a similar shell command.

An output row for every document found in the collection with the result field containing a BSON representation of the document.

db.collection.find( BSON *query* )

Finds the documents in a collection matching the provided query. For example: db.identities.find( { superhero : { $gt : "S" } } ); See db.collection.find for details on query formation from a similar shell command.

An output row for every document found in the collection with the result field containing a BSON representation of the document.

db.collection.insertMany( [BSON, ...] *documents* )

Insert documents into the collection. For example: db.identities.insertMany( [ { superhero : "CodeMan" }, { superhero : "ETL Pro" } ] ); See db.collection.insertMany for details on a similar shell command.

An output row with the result field containing a BSON document with an insertedIds array and a Boolean indicating the command was acknowledged.

db.collection.insertMany( [BSON, ...] *documents*, BSON *options* )

Insert documents into the collection with options. Option: { ordered: BOOLEAN }

For example: db.identities.insertMany( [ { superhero : "CodeMan" }, { superhero : "ETL Pro" } ], { ordered : true } );

See db.collection.insertMany for details on a similar shell command.

An output row with the result field containing a BSON document with an insertedIds array and a Boolean indicating the command was acknowledged.

db.collection.insertOne( BSON *document* )

Inserts a document into the collection.For example: db.identities.insertOne( { superhero : "CodeMan" } ); See db.collection.insertOne for details on a similar shell command.

An output row with the result field containing a BSON document with the insertedId and a Boolean indicating the command was acknowledged.

db.collection.mapReduce( STRING *map*, STRING *reduce* )

Performs a mapReduce on the collection using the given map and reduce functions provided as Strings (surrounded by quotes). For example: db.identities.mapReduce( "function() { emit( this.superhero, 1 ); }", "function( key, values ) { return values.length; }" ); See db.collection.mapReduce for details on a similar shell command.

A set of output rows with the result field containing a BSON document per row of mapReduce results.

db.collection.updateMany( BSON *filter*, BSON *update* )

Updates documents matching the filter in the collection according to updates specified in the update parameter. For example: db.identities.updateMany( { superhero : "CodeMan" }, { $set : { secret_identity: "John Doe" } } ); See db.collection.updateMany for details on constructing the filter and update parameters from a similar shell command.

An output row with the result field containing a BSON document with the matchedCount, modifiedCount, and a Boolean indicating the command was acknowledged.

db.collection.updateMany( BSON *filter*, BSON *update*, BSON *options* )

Updates documents matching the filter in the collection according to updates specified in the update parameter adhering to the specified options. Option: { upsert: BOOLEAN, arrayFilters: [ BSON, ...] }

For example: db.identities.updateMany( { superhero : "ETL Pro" }, { $set : { secret_identity: "Roger Smith" } }, { upsert : true } );

See db.collection.updateMany for details on constructing the filter and update parameters from a similar shell command.

An output row with the result field containing a BSON document with the matchedCount, modifiedCount, the upsertedId if using the upsert option, and a Boolean indicating the command was acknowledged.

db.collection.updateOne( BSON *filter*, BSON *update* )

Updates at most one top document matching the filter in the collection according to updates specified in the update parameter. For example: db.identities.updateOne( { superhero : "CodeMan" }, { $set : { secret_identity: "John Doe" } } ); See db.collection.updateOne for details on constructing the filter and update parameters from a similar shell command.

An output row with the result field containing a BSON document with the matchedCount, modifiedCount, and a Boolean indicating the command was acknowledged.

db.collection.updateOne( BSON *filter*, BSON *update*, BSON *options* )

Updates at most one top document matching the filter in the collection according to updates specified in the update parameter adhering to the specified options. Option: { upsert: BOOLEAN, arrayFilters: [ BSON, ...] }

For example: db.identities.updateOne( { superhero : "ETL Pro" }, { $set : { secret_identity: "Roger Smith" } }, { upsert : true } );

See db.collection.updateOne for details on constructing the filter and update parameters from a similar shell command.

An output row with the result field containing a BSON document with the matchedCount, modifiedCount, the upsertedId if using the upsert option, and a Boolean indicating the command was acknowledged.

Last updated

Was this helpful?