Overview
This node allows users to execute native MongoDB queries directly within an n8n workflow. It supports complex query operations including date and ObjectId handling, enabling advanced data retrieval from MongoDB collections. Common use cases include fetching specific documents by criteria, aggregating data, or performing custom queries that go beyond simple filters.
For example, you can use this node to:
- Retrieve a single document matching a specific condition (e.g., find one user by email).
- Fetch multiple documents with a limit on the number of results.
- Run aggregation pipelines to compute summaries or transform data.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection to query. |
| Query | The MongoDB query in JSON format. Supports special syntax for dates ($date) and ObjectIds ($oid). For example: {"createdAt": {"$date": "2023-01-01T00:00:00Z"}}. |
| Max Results | Maximum number of results to return (applies to operations that return multiple documents). |
Output
The node outputs an array of items where each item contains a json object with the following fields:
result: The query result(s) returned from MongoDB. This can be a single document (for "Find One") or an array of documents (for "Find" and "Aggregate"). Dates are serialized as ISO strings.query: The parsed query object used for the operation.operation: The operation performed (find,findOne, oraggregate).collection: The name of the collection queried.error(optional): If an error occurs and the node is set to continue on failure, this field contains the error message.
The node does not output binary data.
Dependencies
- Requires a MongoDB connection string and optionally a database name.
- Needs credentials providing access to a MongoDB instance.
- Uses the official MongoDB Node.js driver internally.
- The connection string must include authentication details and target database if not specified separately.
Troubleshooting
- Invalid JSON in Query: If the query property contains malformed JSON, the node will throw an "Invalid JSON" error. Ensure the query string is valid JSON and uses correct syntax for
$dateand$oid. - Database Name Not Found: If no database is specified in credentials or connection string, the node throws an error indicating the database name could not be found. Make sure the connection string includes the database or specify it explicitly.
- Unknown Operation: If an unsupported operation is selected, the node will error out. Use only the supported operations: Find, Find One, Aggregate.
- Connection Errors: Issues connecting to MongoDB (wrong credentials, network issues) will cause errors. Verify credentials and network accessibility.
- Max Results Ignored for Find One: The "Max Results" property does not apply to the "Find One" operation; it is ignored in that context.
To resolve errors, check the error messages in the output and verify the input parameters and credentials accordingly.