Overview
This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports a variety of database operations such as aggregation, querying, updating, deleting, counting, and creating documents within specified collections. The node is particularly useful for users who want to leverage MongoDB's powerful querying capabilities combined with Mongoose's schema validation and modeling features directly within n8n workflows.
A common scenario includes running complex aggregation pipelines to transform and analyze data stored in MongoDB collections, or performing CRUD operations with schema enforcement. For example, you can aggregate user activity logs to generate reports or update multiple documents based on specific criteria.
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 fields in the documents (e.g., "name": "String", "age": "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 | JSON array defining the aggregation pipeline stages to run when the "Aggregate" operation is selected. Each stage corresponds to a MongoDB aggregation operator (e.g., $match, $sort). |
| Options | A collection of additional options affecting query behavior: - Limit: Maximum number of results to return (max 10,000). - Skip: Number of documents to skip (not applicable for aggregate). - Sort: Sort order as JSON (not applicable for aggregate). - Select Fields: Fields to include/exclude as a string (not applicable for aggregate). - Upsert: Whether to create document if it doesn't exist (update only). - Multi: Whether to update multiple documents (update only). - Query Timeout (Ms): Max time allowed for query execution (1-300 seconds). - Debug Mode: Enable Mongoose debug mode to log all MongoDB queries in console. |
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.
- No binary data output is produced by this node.
Example output for an aggregate operation might look like:
{
"json": [
{ "name": "Alice", "email": "alice@example.com" },
{ "name": "Bob", "email": "bob@example.com" }
],
"pairedItem": { "item": 0 }
}
If query info is included, the output may contain additional fields like:
{
"json": {
"data": [ /* aggregation results */ ],
"query": { /* original pipeline */ },
"operation": "aggregate"
},
"pairedItem": { "item": 0 }
}
Dependencies
- Requires a valid MongoDB connection string and credentials configured in n8n.
- Uses the Mongoose ODM library to interact with MongoDB.
- The node expects the MongoDB server to be accessible and properly configured for authentication if required.
- Optional: Enabling debug mode requires console access to view detailed MongoDB query logs.
Troubleshooting
- Connection timeout or failure: Errors like "Connection timeout after 10 seconds" or "MongoDB connection error" indicate network issues or incorrect connection strings. Verify the MongoDB URI, network connectivity, and that the MongoDB server is running.
- Authentication failed: Messages mentioning authentication failure suggest invalid username/password or misconfigured authentication database. Check credentials and authentication settings.
- Host not found: Errors including "ENOTFOUND" or "getaddrinfo" mean the hostname/IP address is unreachable or incorrect.
- Connection refused: Indicates MongoDB is not listening on the specified port or firewall rules block access.
- Invalid JSON format: Input properties expecting JSON (e.g., schema definition, pipeline) must be valid JSON. Malformed JSON will cause parsing errors.
- Limit exceeds maximum: Setting a limit above 10,000 will throw an error to prevent excessive resource usage.
- Large result sets warning: Returning more than 5,000 documents triggers a console warning suggesting adding indexes or refining filters for performance.
To resolve these issues, verify all input parameters, ensure MongoDB server accessibility, and validate JSON inputs carefully.