# 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](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/) 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](https://docs.mongodb.com/manual/reference/method/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](https://docs.mongodb.com/manual/reference/method/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* )`             | <p>Counts the documents in a collection. Option: <code>{ limit: INTEGER, skip: INTEGER, maxTimeMS: LONG }</code></p><p>For example: <code>db.identities.countDocuments( { superhero : { $gt : "S" } }, { limit : 1 } );</code></p><p>See <a href="https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/">db.collection.countDocuments</a> for details on query formation from a similar shell command.</p>                                                                                                                                                                                                                                                           | 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](https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/#mongodb-method-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](https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/#mongodb-method-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](https://docs.mongodb.com/manual/reference/method/db.collection.find/#mongodb-method-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](https://docs.mongodb.com/manual/reference/method/db.collection.find/#mongodb-method-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](https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#mongodb-method-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* )`      | <p>Insert documents into the collection with options. Option: { ordered: BOOLEAN }</p><p>For example: <code>db.identities.insertMany( \[ { superhero : "CodeMan" }, { superhero : "ETL Pro" } ], { ordered : true } );</code></p><p>See <a href="https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#mongodb-method-db.collection.insertMany">db.collection.insertMany</a> for details on a similar shell command.</p>                                                                                                                                                                                                                                                 | 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](https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/#mongodb-method-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](https://docs.mongodb.com/manual/reference/method/db.collection.mapReduce/#mongodb-method-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](https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#mongodb-method-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* )` | <p>Updates documents matching the <em>filter</em> in the collection according to updates specified in the <em>update</em> parameter adhering to the specified <em>options</em>. Option: <code>{ upsert: BOOLEAN, arrayFilters: \[ BSON, ...] }</code></p><p>For example: <code>db.identities.updateMany( { superhero : "ETL Pro" }, { $set : { secret\_identity: "Roger Smith" } }, { upsert : true } );</code></p><p>See <a href="https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany">db.collection.updateMany</a> for details on constructing the filter and update parameters from a similar shell command.</p>            | 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](https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#mongodb-method-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* )`  | <p>Updates at most one top document matching the <em>filter</em> in the collection according to updates specified in the <em>update</em> parameter adhering to the specified <em>options</em>. Option: <code>{ upsert: BOOLEAN, arrayFilters: \[ BSON, ...] }</code></p><p>For example: <code>db.identities.updateOne( { superhero : "ETL Pro" }, { $set : { secret\_identity: "Roger Smith" } }, { upsert : true } );</code></p><p>See <a href="https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">db.collection.updateOne</a> for details on constructing the filter and update parameters from a similar shell command.</p> | 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*. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.pentaho.com/pdia-data-integration/10.2-data-integration/pdi-transformation-steps-reference-overview/mongodb-execute/commands-execute-step/collection-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
