Overview
This node enables performing a wide range of MongoDB operations directly within an n8n workflow. It connects to a specified MongoDB database and collection, then executes the chosen operation on the documents in that collection.
Common scenarios where this node is beneficial include:
- Inserting new documents or batches of documents into a MongoDB collection.
- Querying documents with filters, projections, sorting, and pagination.
- Updating or replacing existing documents based on filter criteria.
- Deleting documents matching certain conditions.
- Running aggregation pipelines for complex data transformations.
- Counting documents or retrieving distinct values for a field.
- Executing bulk write operations for batch processing.
Practical examples:
- Automatically inserting form submissions into a MongoDB collection.
- Fetching user records updated after a certain date.
- Updating status fields of multiple documents in response to an event.
- Aggregating sales data by region using an aggregation pipeline.
- Removing outdated logs from a collection periodically.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection on which to perform the operation. |
| Operation | The MongoDB operation to execute. Options include: insertOne, insertMany, find, findOne, findOneAndUpdate, aggregate, updateOne, updateMany, replaceOne, deleteOne, deleteMany, countDocuments, estimatedDocumentCount, distinct, bulkWrite. |
| Field Name | (For distinct operation) The field name for which to find distinct values. |
| Filter | A JSON object specifying the query filter to select documents for operations like find, update, delete, count, distinct, etc. |
| Pipeline | (For aggregate operation) An array of aggregation pipeline stages as JSON objects. |
| Document | (For insertOne operation) The single document to insert as a JSON object. |
| Documents | (For insertMany operation) An array of documents to insert. |
| Update | (For update operations) The update instructions as a JSON object, e.g., { "$set": { ... } }. |
| Replacement | (For replaceOne operation) The replacement document as a JSON object. |
| Operations | (For bulkWrite operation) An array of write operations to perform in bulk. |
Options collections for specific operations:
findOneOptions (for
findOne):- Projection (JSON): Fields to include or exclude.
- Sort (JSON): Sorting order.
- Hint (string): Index hint.
- Collation (string): Collation settings.
- Batch Size (number): Number of documents per batch.
findOptions (for
find):- Projection (JSON)
- Sort (JSON)
- Skip (number): Number of documents to skip.
- Limit (number): Max number of results.
- Hint (string)
- Collation (string)
- Batch Size (number)
updateOptions (for
updateOne,updateMany):- ArrayFilters (number)
- Hint (string)
- Upsert (boolean)
findOneAndUpdateOptions (for
findOneAndUpdate):- ArrayFilters (number)
- ReturnDocument (before/after)
- Hint (string)
- Upsert (boolean)
Output
The node outputs the original input items enriched with a json property containing a result field. This result holds the outcome of the MongoDB operation executed for each item.
- For query operations (
find,findOne,aggregate,distinct),resultcontains the retrieved documents or values. - For insert/update/delete operations,
resultincludes the MongoDB response object detailing the operation's success and metadata (e.g., inserted IDs, matched counts). - For count operations,
resultis the numeric count. - For bulk operations,
resultcontains the bulk write result summary.
If the operation fails for an item and "Continue On Fail" is enabled, the error is attached to the item; otherwise, execution stops with an error.
The node does not output binary data.
Dependencies
- Requires a valid connection to a MongoDB database via an API key credential or connection string.
- The node expects credentials providing a MongoDB connection string and database name.
- Uses the official MongoDB Node.js driver internally.
- No additional environment variables are required beyond the MongoDB credentials configured in n8n.
Troubleshooting
- Connection errors: Ensure the MongoDB connection string and database name are correct and accessible from the n8n environment.
- Invalid JSON inputs: Filters, updates, pipelines, and other JSON parameters must be valid JSON. Syntax errors will cause failures.
- Operation-specific errors: For example, attempting to update a non-existent document without upsert enabled will result in no changes.
- Index hints: Providing incorrect index names in the "Hint" option may cause query failures.
- Batch size and limits: Setting very large batch sizes or limits can impact performance or cause timeouts.
- Transaction usage: If "Run in Transaction" is enabled but the MongoDB deployment does not support transactions (e.g., standalone server), operations will fail.
To resolve errors, verify parameter correctness, check MongoDB server logs, and ensure network connectivity.