Overview
This node performs MongoDB operations using Extended JSON (EJSON) formatted queries, enabling advanced querying and manipulation of MongoDB documents with support for special BSON types like ObjectId and Date. It is particularly useful when working with complex MongoDB data structures that require precise query syntax beyond standard JSON.
For the Delete operation specifically, the node deletes multiple documents from a specified MongoDB collection based on a user-defined EJSON query filter. This is beneficial in scenarios where you need to remove records matching certain criteria, such as deleting all expired sessions, removing test data, or cleaning up entries by specific attributes.
Example use case:
You want to delete all documents in the "users" collection where the field status equals "inactive":
{
"status": "inactive"
}
Using this node, you provide this query in EJSON format, and it will delete all matching documents efficiently.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection where the delete operation will be performed. |
| Delete Query (EJSON Format) | The MongoDB delete query expressed in EJSON format. Supports special BSON types like ObjectId ({"$oid": "..."}) and Date ({"$date": "..."}). Defines which documents to delete. |
| Lean Output | Boolean flag indicating whether to return lean output. When true, MongoDB ObjectIds are removed and plain objects are returned. |
Output
The output JSON contains the result of the delete operation with the following fields:
deletedCount: Number of documents deleted.acknowledged: Boolean indicating if the delete operation was acknowledged by MongoDB.
If Lean Output is enabled, the output will be simplified to plain JavaScript objects without MongoDB-specific BSON wrappers.
No binary data output is produced by this operation.
Dependencies
- Requires a valid connection to a MongoDB database, authenticated via an API key credential or connection string.
- The node depends on the MongoDB Node.js driver and BSON parsing libraries to handle EJSON queries.
- Proper configuration of the MongoDB credentials within n8n is necessary for successful execution.
Troubleshooting
Invalid EJSON query error:
If the provided delete query is not valid EJSON, the node throws an error likeInvalid EJSON query: .... Ensure your query is correctly formatted and uses proper EJSON syntax for special types.Database or collection does not exist:
Errors may occur if the specified database or collection does not exist. Verify the collection name and database credentials.Permission issues:
If the MongoDB user lacks delete permissions on the target collection, the operation will fail. Confirm that the credentials have appropriate privileges.Empty or missing query:
Providing an empty query{}will delete all documents in the collection. Use caution to avoid accidental mass deletions.Connection errors:
Network or authentication failures will prevent the node from connecting to MongoDB. Check network connectivity and credential correctness.
Links and References
- MongoDB Extended JSON Documentation
- MongoDB Delete Operation
- n8n MongoDB Integration Guide (general reference)