MongoDB EJSON icon

MongoDB EJSON

MongoDB operations with EJSON query parsing support

Overview

This node performs MongoDB operations with support for Extended JSON (EJSON) query parsing, enabling advanced queries and data manipulation using MongoDB's native formats such as ObjectId and Date. The "Aggregate" operation specifically allows users to run aggregation pipelines on a specified MongoDB collection, which is useful for transforming and analyzing data within the database.

Common scenarios where this node is beneficial include:

  • Summarizing data by grouping and counting documents based on certain fields.
  • Filtering and reshaping data sets before further processing.
  • Performing complex data transformations directly in MongoDB without needing additional code.

For example, you could use the Aggregate operation to count active users per category by providing a pipeline like:

[
  { "$match": { "status": "active" } },
  { "$group": { "_id": "$category", "count": { "$sum": 1 } } }
]

Properties

Name Meaning
Collection The name of the MongoDB collection on which the aggregation pipeline will be executed.
Pipeline (EJSON Format) The MongoDB aggregation pipeline expressed in EJSON format. This is an array of pipeline stages that define the aggregation steps. Supports MongoDB types like ObjectId and Date.
Lean Output Boolean flag indicating whether to return lean output. When true, MongoDB-specific objects like ObjectId are converted to plain JavaScript objects for easier consumption.

Output

The output is an array of JSON objects representing the results of the aggregation pipeline. Each object corresponds to a document returned by the pipeline stages.

  • If "Lean Output" is enabled, MongoDB-specific types such as ObjectId and Date are converted into plain objects or strings for easier handling downstream.
  • The output does not include binary data for this operation.

Example output structure:

[
  {
    "_id": "category1",
    "count": 10
  },
  {
    "_id": "category2",
    "count": 5
  }
]

Dependencies

  • Requires a valid connection to a MongoDB database, authenticated via an API key credential or connection string.
  • The node depends on MongoDB driver libraries and BSON utilities for parsing and serializing EJSON.
  • Proper configuration of the MongoDB credentials in n8n is necessary for successful execution.

Troubleshooting

  • Invalid EJSON query error: If the pipeline JSON is malformed or contains invalid EJSON syntax, the node will throw an error indicating the issue. Ensure the pipeline is valid JSON and uses correct EJSON formatting for special types.
  • Pipeline must be an array: The aggregation pipeline must be provided as an array of stages. Passing other types will cause an error.
  • Database or collection not found: If the specified database or collection does not exist, the node will fail. Verify the collection name and database credentials.
  • Connection issues: Network or authentication problems with MongoDB will prevent the node from connecting. Check credentials and network access.
  • Empty results: If the pipeline filters out all documents, the output will be an empty array, which is expected behavior.

Links and References

Discussion