MongoDB EJSON icon

MongoDB EJSON

MongoDB operations with EJSON query parsing support

Overview

This node enables performing various MongoDB operations using Extended JSON (EJSON) formatted queries and data. It supports querying, inserting, updating, deleting, aggregating, and counting documents in a specified MongoDB collection with rich EJSON support, including handling of ObjectId and Date types.

Common scenarios include:

  • Retrieving filtered data from MongoDB collections with complex queries.
  • Inserting new documents with precise control over BSON types.
  • Updating or replacing existing documents based on keys.
  • Deleting documents matching specific criteria.
  • Running aggregation pipelines for advanced data processing.
  • Counting documents matching certain conditions.

Practical example: You want to fetch user records created after a certain date, sorted by creation time, limiting results to 50, and excluding the internal MongoDB _id field. Using this node's "Find" operation with an EJSON query and projection makes this straightforward.

Properties

Name Meaning
Collection The name of the MongoDB collection to operate on.
Query (EJSON Format) The MongoDB find query expressed in EJSON format. Supports special BSON types like ObjectId ({"$oid": "..."}) and Date ({"$date": "..."}). Defines which documents to retrieve. Required for Find operation.
Options Additional query options for the Find operation:
- Lean Output: Return plain JavaScript objects instead of MongoDB BSON objects (removes ObjectId).
- Limit: Maximum number of documents to return.
- Projection (EJSON Format): Fields to include or exclude in results.
- Skip: Number of documents to skip.
- Sort (EJSON Format): Sort order for the results.
Lean Output Whether to return lean output (plain objects without MongoDB ObjectId wrappers). Applies to multiple operations including Find. Defaults to true.

Output

The node outputs an array of items where each item contains a json property representing a document or result object from MongoDB. For the Find operation, each item corresponds to one document matching the query, transformed according to the projection and other options.

If "Lean Output" is enabled, the output documents are plain JavaScript objects with MongoDB-specific BSON types (like ObjectId) converted to strings or standard JSON representations.

No binary data output is produced by this node.

Dependencies

  • Requires a valid connection to a MongoDB database, authenticated via an API key credential or connection string configured in n8n.
  • Uses MongoDB Node.js driver and BSON library internally.
  • The node expects the MongoDB server to be accessible and the specified database and collection to exist.

Troubleshooting

  • Invalid EJSON query error: If the EJSON query or projection JSON is malformed or contains unsupported syntax, the node will throw an error indicating invalid EJSON. Ensure your JSON is valid and uses correct EJSON syntax for special types.
  • Database does not exist: If the specified database is not found on the MongoDB server, the credential test will fail with a warning. Verify the database name and connection credentials.
  • Empty results: If no documents match the query, the output will be empty. Double-check your query filters and ensure documents exist that satisfy them.
  • Connection issues: Network problems or incorrect connection strings can cause failures connecting to MongoDB. Confirm connectivity and credentials.
  • Unsupported operation: If an unknown operation is specified, the node throws an error. Use only supported operations as documented.

Links and References

Discussion