Overview
This node enables interaction with a MongoDB database, specifically allowing users to perform various operations such as finding, inserting, updating, deleting, and aggregating documents within specified collections. The "Find" operation retrieves documents from a MongoDB collection based on a user-defined query and optional parameters like sorting, limiting, and skipping results.
Common scenarios for this node include:
- Retrieving filtered data sets from a MongoDB collection for further processing or analysis.
- Querying large datasets with pagination (using skip and limit).
- Sorting query results by one or more fields.
- Integrating MongoDB data retrieval into automated workflows, e.g., fetching user records created after a certain date.
Practical example:
- A workflow that fetches all customer records born after 1950, sorted by last name descending, limited to 50 results per execution.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection to query. |
| Query | A JSON-formatted MongoDB find query specifying the filter criteria for documents to retrieve. |
| Options | Additional query options: |
| - Limit | Maximum number of documents to return; 0 means no limit. |
| - Skip | Number of documents to skip before returning results (useful for pagination). |
| - Sort | JSON object defining the sort order of the results, e.g., { "field": -1 } to sort descending by field. |
Output
The output is an array of JSON objects representing the documents retrieved from the MongoDB collection matching the query and options. Each item corresponds to one document.
- The
jsonfield contains the document's data. - If the query returns no documents, the output will be an empty array.
- The node does not output binary data.
Dependencies
- Requires a connection to a MongoDB database, authenticated via credentials containing either a connection string or connection parameters.
- The node depends on the MongoDB Node.js driver and BSON utilities for parsing queries and handling ObjectId types.
- Proper configuration of the MongoDB credentials in n8n is necessary for successful connection.
Troubleshooting
- Invalid Query JSON: If the query or sort JSON is malformed, the node will throw a parsing error. Ensure the JSON syntax is correct.
- Non-existent Collection or Database: Errors occur if the specified collection or database does not exist. Verify names are correct.
- ObjectId Handling: When querying by
_id, ensure the value is a valid ObjectId string; otherwise, the node attempts to convert it but may fail. - Connection Issues: Authentication failures or network issues will prevent the node from connecting. Check credentials and network access.
- Limit and Skip Values: Negative values for limit or skip may cause errors or unexpected behavior. Use zero or positive integers.
- Empty Results: If no documents match the query, the output will be empty; this is expected behavior, not an error.
To resolve errors, review the error message returned by the node, adjust the query or options accordingly, and verify MongoDB connectivity.