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 creating, finding, updating, deleting documents, counting documents, and running aggregation pipelines on specified collections.
Typical use cases include:
- Deleting multiple documents matching a query filter.
- Removing a single document based on specific criteria.
- Managing data in MongoDB collections within automated workflows.
- Running complex queries or aggregations to transform or analyze data.
For example, the "Delete" operation can be used to remove all user records older than a certain date, helping maintain clean and relevant datasets.
Properties
| Name | Meaning |
|---|---|
| Database | Name of the MongoDB database to connect to. Leave empty to use the default from credentials. |
| Collection | Name of the collection on which the operation will be performed. |
| Query | MongoDB query object in JSON format to filter documents for deletion. Leave empty for no filter. |
| Schema Definition | JSON definition of the Mongoose schema for the target collection, specifying field types. |
| Include Query Info | Boolean flag to include raw MongoDB query information in the response for debugging or logging. |
| Options | Additional options for the operation: |
| - Limit | Maximum number of results to return (not applicable for delete but present in options). |
| - Skip | Number of documents to skip (not applicable for delete). |
| - Sort | Sort order for results (not applicable for delete). |
| - Select Fields | Fields to include or exclude in results (not applicable for delete). |
| - Upsert | Whether to create a document if it doesn't exist (only for update operations). |
| - Multi | Whether to update multiple documents (only for update operations). |
| - Query Timeout (Ms) | Maximum time allowed for the query to run, between 1000 ms and 300000 ms (1-300 seconds). |
| - Debug Mode | Enables Mongoose debug mode to log all MongoDB queries to the console. |
Output
The output is an array of JSON objects representing the result of the delete operation(s). For the "Delete" operation:
- The
jsonoutput contains the result of the deletion command, typically including fields like the count of deleted documents. - If "Include Query Info" is enabled, the output also includes the original query and options used.
- In case of errors, the output may contain an
errorfield describing the issue.
No binary data is produced by this node.
Example output structure for a successful delete operation:
{
"deletedCount": 5,
"query": { "age": { "$lt": 18 } },
"operation": "delete"
}
Dependencies
- Requires a valid connection to a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM for schema definition and database operations.
- Requires an API key credential or similar authentication token configured in n8n to access the MongoDB database.
- The node expects the user to provide a valid Mongoose schema definition for the target collection.
Troubleshooting
Common Issues
- Connection errors: Occur if the MongoDB server is unreachable, the hostname is incorrect, or the port is closed.
- Authentication failures: Happen when credentials are invalid or missing required permissions.
- Invalid JSON in query or schema: The node parses JSON strings for queries and schemas; malformed JSON will cause errors.
- Exceeding limits: Setting very high limits for queries might be rejected for safety reasons.
- Timeouts: Queries taking longer than the specified timeout will fail.
Error Messages and Resolutions
"MongoDB connection error": Verify the connection string, ensure MongoDB is running and accessible."MongoDB authentication failed": Check username, password, and authentication database settings."MongoDB host not found": Confirm the hostname/IP address and network connectivity."MongoDB connection refused": Ensure MongoDB service is running on the specified port."Invalid JSON format": Correct the JSON syntax in query or schema inputs."Limit exceeds maximum allowed limit": Reduce the limit value to within allowed range.
Enabling "Debug Mode" can help trace issues by logging detailed MongoDB queries to the console.