Overview
This node allows users to interact with a MongoDB database by performing various operations such as finding, inserting, updating, deleting, and aggregating documents within specified collections. Specifically, the Delete operation removes multiple documents from a chosen MongoDB collection based on a user-defined query.
Common scenarios for this node include:
- Cleaning up outdated or irrelevant data in a MongoDB collection.
- Automating deletion of records that meet certain criteria (e.g., removing users inactive since a specific date).
- Managing data lifecycle by programmatically deleting documents matching complex queries.
For example, you could delete all documents where the "birth" field is greater than "1950-01-01" by providing a JSON query { "birth": { "$gt": "1950-01-01" } }
.
Properties
Name | Meaning |
---|---|
Collection | The name of the MongoDB collection where the delete operation will be performed. |
Delete Query (JSON Format) | A JSON-formatted MongoDB query specifying which documents to delete. For example: { "birth": { "$gt": "1950-01-01" } } . |
Output
The output is a JSON array containing a single object with the property:
deletedCount
: The number of documents that were deleted by the operation.
Example output:
[
{
"deletedCount": 5
}
]
This indicates that 5 documents matched the query and were removed from the collection.
The node does not output binary data.
Dependencies
- Requires a valid connection to a MongoDB database, authenticated via an API key credential or similar authentication method configured in n8n.
- The node depends on the official MongoDB Node.js driver and BSON utilities for parsing queries and handling ObjectId types.
- The user must provide correct MongoDB credentials and specify the target database and collection.
Troubleshooting
Common issues:
- Invalid JSON format in the delete query can cause parsing errors.
- Specifying a non-existent collection or database will result in connection or operation errors.
- Insufficient permissions on the MongoDB user may prevent deletion.
- Using string values for
_id
fields without converting them to ObjectId may lead to no documents being matched.
Error messages:
"Database \"<name>\" does not exist"
: Check that the database name is correct and accessible.- MongoDB query parse errors: Ensure the delete query is valid JSON and uses correct MongoDB query syntax.
- Permission denied errors: Verify that the provided credentials have delete permissions on the target collection.
To handle errors gracefully, enable the node's "Continue On Fail" option to allow workflow execution to proceed even if deletion fails, capturing error messages in the output.