Overview
This node enables interaction with a MongoDB database, allowing users to perform various operations such as finding, inserting, updating, deleting, and aggregating documents within specified collections. The "Find" operation specifically retrieves documents from a MongoDB collection based on a query filter, with options to limit, skip, and sort the results.
Common scenarios for this node include:
- Retrieving user data filtered by specific criteria (e.g., users born after a certain date).
- Fetching records with pagination support using skip and limit.
- Sorting query results by one or more fields in ascending or descending order.
Practical example:
- Find all customers in a "customers" collection who signed up after January 1, 2020, limit the results to 100, skip the first 10, and sort them by signup date descending.
Properties
Name | Meaning |
---|---|
Collection | The name of the MongoDB collection to query. |
Query | A JSON-formatted MongoDB find query that specifies the filter criteria for documents to retrieve. |
Options | Additional query options including: |
- Limit | Maximum number of documents to return; use 0 for no limit. |
- Skip | Number of documents to skip before returning results (useful for pagination). |
- Sort | JSON object defining the sort order of the result set, e.g., { "field": -1 } for descending. |
Output
The output is an array of JSON objects representing the documents retrieved from the MongoDB collection matching the query criteria. Each object corresponds to a document in the collection.
If any errors occur during execution and the node is configured to continue on failure, the output will contain objects with an error
field describing the issue.
No binary data is output by this operation.
Dependencies
- Requires a valid connection to a MongoDB instance, authenticated via credentials containing necessary connection details (such as connection string or parameters).
- The node depends on the MongoDB Node.js driver and BSON utilities for parsing queries and handling ObjectId conversions.
- Proper configuration of the MongoDB credentials in n8n is required to establish the connection.
Troubleshooting
- Invalid Query Format: If the JSON query is malformed, the node will throw a parsing error. Ensure the query is valid JSON and follows MongoDB query syntax.
- Invalid ObjectId Strings: Queries filtering by
_id
must provide valid ObjectId strings; otherwise, conversion will fail. - Connection Issues: Errors connecting to MongoDB may arise due to incorrect credentials, network issues, or missing database. Verify credentials and network accessibility.
- Empty Results: If no documents match the query, the output will be an empty array.
- Unsupported Operation: Using an operation other than those implemented will cause an error indicating unsupported operation.
To resolve errors:
- Validate JSON inputs carefully.
- Confirm the MongoDB server is reachable and credentials are correct.
- Use the "Continue On Fail" option to handle errors gracefully without stopping workflow execution.