Overview
This node performs various MongoDB operations on specified collections within a database. It is useful for automating data manipulation tasks such as updating documents, querying data, aggregating results, inserting new documents, replacing existing ones, or deleting documents in MongoDB.
A common scenario includes updating user records based on certain criteria, e.g., setting a new birthdate for users born after 1950. Another example is retrieving filtered data using queries or performing aggregation pipelines to summarize data.
The "Update" operation specifically allows modifying one or more documents matching a query with specified update instructions and options.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection where the operation will be performed. |
| Query (JSON Format) | A JSON-formatted MongoDB find query that specifies which documents to target for the operation. |
| Update | JSON object specifying the update operations to apply to matched document(s), e.g., $set. |
| Update Options | Additional JSON options for the update operation, such as upsert or array filters. |
Output
The output JSON structure for the "Update" operation includes:
success: Boolean indicating if the update was successful.operation: String"update"indicating the type of operation performed.modifiedCount: Number representing how many documents were modified (1 if a document was updated, 0 otherwise).data: The updated document returned after the update operation, ornullif no document matched.
Example output JSON:
{
"success": true,
"operation": "update",
"modifiedCount": 1,
"data": {
"_id": "someObjectId",
"birth": "1960-01-01",
...
}
}
No binary data output is produced by this operation.
Dependencies
- Requires a valid connection to a MongoDB instance via a connection string and database name.
- Needs an API key credential or authentication token configured in n8n to connect securely to MongoDB.
- Uses the official MongoDB Node.js driver internally.
Troubleshooting
Common issues:
- Malformed JSON in the "Query" or "Update" fields can cause parsing errors.
- Incorrect collection names or missing collections will result in operation failures.
- Insufficient permissions or invalid credentials will prevent connection to MongoDB.
- Using unsupported update operators or invalid update syntax may cause errors.
Error messages:
- Parsing errors when JSON is invalid: Ensure the JSON format is correct and properly escaped.
- Connection errors: Verify the connection string, credentials, and network access to MongoDB.
- No documents matched: The
modifiedCountwill be 0; check the query criteria. - Operation errors from MongoDB: Review the update syntax and options for correctness.
To handle errors gracefully, enable the "Continue On Fail" option to allow the workflow to proceed even if some updates fail.