Overview
This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports various database operations such as aggregation, querying, updating, deleting, counting, and creating documents within a specified collection. The node is particularly useful for workflows that require complex data manipulation or retrieval from MongoDB collections, leveraging Mongoose's schema definitions to enforce structure.
A common scenario includes running aggregation pipelines to transform and analyze data directly in MongoDB, such as grouping sales data by region or filtering user records based on multiple criteria. Another example is updating multiple documents matching certain conditions or retrieving specific fields from documents efficiently.
Properties
| Name | Meaning |
|---|---|
| Database | Name of the MongoDB database to use. If left empty, the default database from the configured credential will be used. |
| Collection | The name of the MongoDB collection on which the operation will be performed. |
| Schema Definition | JSON definition of the Mongoose schema for the collection. Defines the structure and types of the documents (e.g., fields like "name" as String, "age" as Number). |
| Include Query Info | Boolean flag indicating whether to include raw MongoDB query information in the response output. Useful for debugging or logging purposes. |
| Pipeline | An array of aggregation pipeline stages (in JSON format) to run when performing the Aggregate operation. Each stage defines a step in the aggregation process (e.g., $match, $sort, $limit, $project). |
| Options | A collection of additional options applicable to various operations: |
| - Limit | Maximum number of results to return (default 1000). Applies to find-type operations but not aggregate. |
| - Skip | Number of documents to skip in the result set (not applicable for aggregate operations). |
| - Sort | JSON object defining sort order (e.g., {"name": 1, "age": -1}), not applicable for aggregate operations. |
| - Select Fields | Space-separated string listing fields to include or exclude (e.g., "name email -_id"), not applicable for aggregate operations. |
| - Upsert | Boolean indicating whether to create a document if it doesn't exist (only for update operations). |
| - Multi | Boolean indicating whether to update multiple documents (only for update operations). |
| - Query Timeout (Ms) | Maximum time in milliseconds to allow the query to run (range 1000-300000 ms). |
| - Debug Mode | Enables Mongoose debug mode to log all MongoDB queries to the console for troubleshooting. |
Output
The node outputs an array of items where each item contains a json field representing the result of the executed operation:
- For Aggregate operation, the
jsonfield contains an array of documents resulting from the aggregation pipeline. - If
Include Query Infois enabled, the output also includes metadata about the executed query or pipeline. - The output structure varies depending on the operation but generally includes the data returned by MongoDB after applying the requested operation.
- Binary data output is not supported by this node.
Example output for an aggregate operation might look like:
[
{
"json": [
{ "name": "Alice", "totalSales": 1500 },
{ "name": "Bob", "totalSales": 1200 }
]
}
]
Dependencies
- Requires a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library to interact with MongoDB.
- Requires configuration of credentials containing the MongoDB connection details.
- Supports multi-database connections by specifying the database name optionally.
- No additional external services are required beyond MongoDB.
Troubleshooting
Connection Errors:
Errors like "MongoDB connection error," "authentication failed," "host not found," or "connection refused" indicate issues with the connection string, credentials, network, or MongoDB server availability. Verify the connection string, username/password, host/IP, port, and ensure MongoDB is running and reachable.Timeouts:
Queries may timeout if they take longer than the specified maximum time (Query Timeout (Ms)). Increase the timeout or optimize your queries/pipelines.Invalid JSON Format:
Input properties expecting JSON (e.g., schema definition, pipeline) must be valid JSON. Invalid JSON strings will cause errors. Use proper JSON formatting.Limit Exceeded:
The node enforces a maximum limit of 10,000 documents for safety. Requests exceeding this limit will throw an error.Large Result Sets Warning:
When returning more than 5,000 documents, a warning is logged suggesting adding indexes or refining filters to improve performance.Schema Issues:
Incorrect schema definitions can cause unexpected behavior. Ensure the schema matches the actual collection structure.
Links and References
- Mongoose Documentation โ Official guide for defining schemas and models.
- MongoDB Aggregation Pipeline โ Details on building aggregation pipelines.
- MongoDB Query Operators โ Reference for query operators used in filters.
- n8n Documentation โ General documentation for n8n nodes and workflows.