Overview
This node enables interaction with a MongoDB database, specifically allowing users to find documents within a specified collection. It supports querying documents using MongoDB's query syntax and offers options to limit, skip, and sort the results. This is useful in scenarios where you need to retrieve data from MongoDB based on specific criteria, such as filtering user records by age or sorting orders by date.
For example, you could use this node to:
- Retrieve all customers born after 1950.
- Fetch the latest 10 entries from a log collection sorted by timestamp.
- Skip the first 5 documents and get the next 50 matching a certain condition.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection to query. |
| Query (JSON Format) | A JSON object defining the MongoDB find query to filter documents. Example: { "birth": { "$gt": "1950-01-01" } } |
| Options | Additional query options including: |
| - Limit | Maximum number of documents to return; set to 0 for no limit. |
| - Skip | Number of documents to skip before returning results. |
| - Sort (JSON Format) | JSON object defining the sort order of the results. Example: { "field": -1 } where -1 means descending order. |
Output
The output is an array of JSON objects representing the documents retrieved from the MongoDB collection that match the query criteria. Each object corresponds to one document from the database.
If any errors occur during execution and the node is configured to continue on failure, the output will contain objects with an error property describing the issue.
No binary data output is produced by this operation.
Dependencies
- Requires a valid connection to a MongoDB instance.
- Needs credentials providing access to the MongoDB database (such as a connection string or other authentication method).
- Uses BSON parsing to handle MongoDB-specific data types like ObjectId.
- The node depends on the MongoDB Node.js driver and BSON utilities bundled with n8n.
Troubleshooting
- Invalid Query Syntax: If the JSON query or sort options are malformed, the node will throw an error. Ensure the JSON is valid and follows MongoDB query syntax.
- Collection Not Found: If the specified collection does not exist, the query will fail.
- Authentication Errors: Incorrect or missing credentials will prevent connection to MongoDB.
- ObjectId Conversion Issues: When querying by
_id, if the value is a string, it is automatically converted to an ObjectId. Providing invalid ObjectId strings will cause errors. - To handle errors gracefully, enable the "Continue On Fail" option so the workflow can proceed even if some queries fail.