Overview
This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports a variety of operations such as creating, finding, updating, deleting documents, counting documents, and running aggregation pipelines on specified collections within a MongoDB database.
Common scenarios where this node is beneficial include:
- Managing user data by creating or updating user profiles.
- Querying specific documents based on complex filters.
- Deleting outdated or irrelevant records.
- Aggregating data for reports or analytics.
- Counting documents matching certain criteria.
For example, you could use this node to delete all users who have not logged in for over a year by specifying a query filter on the last login date and choosing the "Delete" operation.
Properties
| Name | Meaning |
|---|---|
| Database | The name of the MongoDB database to connect to. Leave empty to use the default database from the configured credential. |
| Collection | The name of the collection within the database on which to perform the operation. |
| Query | A MongoDB query object in JSON format used to filter documents for operations like find, update, delete, and count. Leave empty ({}) to apply no filter (affects all documents). |
| Schema Definition | Defines the Mongoose schema for the target collection in JSON format. This schema informs how documents are structured and typed. |
| Include Query Info | Boolean flag indicating whether to include raw MongoDB query information in the output response. Useful for debugging or logging purposes. |
| Options | A collection of additional options affecting query behavior: |
| - Limit | Maximum number of results to return (default 1000, max 10,000). |
| - Skip | Number of documents to skip before returning results (not applicable for aggregate operations). |
| - Sort | JSON object defining sort order, e.g., {"name": 1, "age": -1} (not applicable for aggregate operations). |
| - Select Fields | String listing fields to include or exclude, e.g., "name email -_id" (not applicable for aggregate operations). |
| - Upsert | For update operations only; if true, creates a new document if none match the query. |
| - Multi | For update operations only; if true, updates multiple documents matching the query instead of just one. |
| - Query Timeout (Ms) | Maximum time in milliseconds to allow the query to run (range 1000 to 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, each containing a json field with the result of the operation:
- For Delete operation (the focus here), the output contains the result of the deletion command, typically including fields such as the number of documents deleted.
- If
Include Query Infois enabled, the output also includes the original query and options used. - In case of errors during processing individual items, the error message is included in the output JSON under an
errorfield if "Continue On Fail" is enabled; otherwise, execution stops with an error.
No binary data output is produced by this node.
Example output structure for a delete operation might look like:
{
"result": {
"deletedCount": 5,
"acknowledged": true
},
"query": { /* the MongoDB query used */ }
}
Dependencies
- Requires a valid connection to a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library for schema definition and database operations.
- Requires configuration of credentials that provide the MongoDB connection string and optionally default database.
- Supports multi-database setups by allowing specification of the target database name.
Troubleshooting
Common Issues
- Connection errors: If the node cannot connect to MongoDB, it throws errors related to connection timeout, refused connections, or host not found.
- Authentication failures: Errors occur if username, password, or authentication source settings are incorrect.
- Invalid JSON input: Query, schema definition, or other JSON inputs must be valid JSON strings or objects; invalid JSON will cause parsing errors.
- Exceeding limits: Setting a limit above 10,000 results in an error to prevent excessive resource usage.
- Large result sets: Returning very large numbers of documents may cause performance issues; consider adding filters or indexes.
Error Messages and Resolutions
- "MongoDB connection error": Verify your connection string, ensure MongoDB server is running and reachable.
- "MongoDB authentication failed": Check credentials and authentication parameters.
- "MongoDB host not found": Confirm hostname/IP and network connectivity.
- "MongoDB connection refused": Ensure MongoDB is listening on the specified port.
- "Invalid JSON format": Correct the JSON syntax in query, schema, or other JSON fields.
- "Limit exceeds maximum allowed": Reduce the limit value to 10,000 or less.
Links and References
- Mongoose Documentation – For schema definitions and query options.
- MongoDB Query Operators – To build valid query objects.
- MongoDB Aggregation Pipeline – For advanced data aggregation.
- n8n Documentation – General guidance on using nodes and workflows.