Overview
This node performs MongoDB operations with support for Extended JSON (EJSON) query parsing. Specifically, the Count operation counts documents in a specified MongoDB collection that match a given EJSON-formatted query. It supports advanced MongoDB data types like ObjectId and Date through EJSON syntax.
Common scenarios include:
- Quickly determining how many documents meet certain criteria without retrieving them.
- Filtering by complex queries involving dates or object IDs.
- Using skip and limit options to count within subsets of data.
Practical example:
- Counting all active users in a "users" collection with a query like
{"status": "active"}. - Counting documents created after a specific date using EJSON date format:
{"createdAt": {"$date": "2023-01-01T00:00:00Z"}}.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection to perform the count on. |
| Count Query (EJSON Format) | The MongoDB count query written in EJSON format. Supports special types like ObjectId ({"$oid": "..."}) and Date ({"$date": "..."}). Example: {"status": "active"}. |
| Options | Additional options for counting: - Limit: Maximum number of results to consider. - Skip: Number of documents to skip before counting. |
Output
The output is a single JSON object containing the count result as a number. For example:
{
"json": 123
}
Where 123 is the number of documents matching the query and options.
No binary data is output by this operation.
Dependencies
- Requires a MongoDB database connection configured via an API key credential or connection string.
- The node uses MongoDB's native driver and BSON EJSON parser to handle queries.
- No additional external services are required beyond MongoDB itself.
Troubleshooting
- Invalid EJSON query error: If the query string is not valid EJSON, the node throws an error indicating invalid EJSON. Ensure the query is properly formatted and uses correct EJSON syntax for special types.
- Database or collection not found: Errors may occur if the specified database or collection does not exist. Verify the connection credentials and collection name.
- Limit and Skip values: Providing negative or zero values for limit or skip may cause unexpected behavior. Use positive integers.
- Connection issues: Network or authentication problems with MongoDB will cause connection errors. Check credentials and network access.
Links and References
- MongoDB Count Documents
- MongoDB Extended JSON (EJSON)
- n8n Documentation on MongoDB Nodes (general reference)