Overview
This node allows users to execute native MongoDB queries directly within an n8n workflow. It supports three main operations on a specified MongoDB collection: finding multiple documents, finding a single document, and performing aggregation pipelines. The node can handle complex queries including date and ObjectId types by parsing special JSON formats.
Common scenarios include:
- Retrieving filtered data from a MongoDB collection based on custom criteria.
- Fetching a single document for detailed inspection or further processing.
- Running aggregation pipelines to transform or summarize data within MongoDB before passing results downstream.
For example, you could use this node to query user records created after a certain date, fetch a specific order by its ID, or aggregate sales data grouped by region.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection to query. |
| Query | The MongoDB query in JSON format. Supports special $date for ISO dates and $oid for ObjectIds. This defines the filter or aggregation pipeline depending on the operation. |
| Max Results | Maximum number of results to return (applies to find and aggregate operations). |
Output
The node outputs an array of items where each item contains a json object with the following fields:
result: The result of the MongoDB operation. For "find" and "aggregate", this is an array of documents; for "findOne", it is a single document or null if none found. All Date objects are converted to ISO string format.query: The parsed query or aggregation pipeline used.operation: The operation performed (find,findOne, oraggregate).collection: The name of the collection queried.
If an error occurs and the node is set to continue on failure, the output will contain an error field with the error message instead of result.
The node does not output binary data.
Dependencies
- Requires a MongoDB connection string and optionally a database name.
- Needs credentials that provide access to a MongoDB instance.
- Uses the official MongoDB Node.js driver internally.
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 name is provided explicitly and cannot be inferred from the connection string, an error will occur. Make sure the connection string includes the database or specify it in credentials.
- Unknown Operation: If an unsupported operation is selected, the node will throw an error indicating the unknown operation.
- Connection Issues: Errors connecting to MongoDB may arise due to incorrect credentials, network issues, or server unavailability.
- Max Results Limit: Setting
Max Resultstoo high might impact performance; adjust according to your needs.
To resolve errors, verify credentials, check query syntax, and ensure the MongoDB server is accessible.