MongoDB EJSON icon

MongoDB EJSON

MongoDB operations with EJSON query parsing support

Overview

This node performs MongoDB operations with support for EJSON (Extended JSON) query parsing. It allows users to interact with a MongoDB database by executing various operations such as inserting documents, querying, updating, deleting, aggregating, and counting documents in specified collections.

The "Insert" operation specifically enables inserting one or multiple documents into a chosen MongoDB collection using EJSON format, which supports MongoDB-specific data types like ObjectId, Date, Decimal128, etc.

Common scenarios:

  • Adding new user records or logs into a MongoDB collection.
  • Importing structured data that includes MongoDB-specific types.
  • Automating data ingestion pipelines where data is provided in EJSON format.

Example:
Inserting a document with a date field:

{
  "name": "John",
  "createdAt": { "$date": "2023-01-01T00:00:00Z" }
}

Properties

Name Meaning
Collection The name of the MongoDB collection where the documents will be inserted.
Data (EJSON Format) The document(s) to insert, expressed in EJSON format. Can be a single object or an array of objects. Supports MongoDB extended types like $date, $numberInt, etc. Example: { "name": "John", "age": { "$numberInt": "30" } }.
Lean Output Boolean flag indicating whether to return lean output. When true, MongoDB ObjectIds are converted to plain strings and the output contains plain JavaScript objects without MongoDB-specific wrappers.

Output

The output JSON structure depends on whether a single document or multiple documents were inserted:

  • For a single document insert (insertOne):
    {
      "insertedId": "<ObjectId string>",
      "acknowledged": true
    }
    
  • For multiple documents insert (insertMany):
    {
      "insertedIds": ["<ObjectId string>", ...],
      "insertedCount": <number>,
      "acknowledged": true
    }
    

If Lean Output is enabled, the ObjectId values are converted to their string representations for easier consumption downstream.

No binary data output is produced by this operation.

Dependencies

  • Requires a MongoDB database connection configured via credentials containing the necessary authentication details (e.g., connection string or parameters).
  • Uses the official MongoDB Node.js driver and BSON library for EJSON parsing and MongoDB operations.
  • The node expects valid EJSON input for the data to insert.

Troubleshooting

  • Invalid EJSON error: If the provided data is not valid EJSON, the node throws an error like Invalid EJSON: <details>. Ensure the input JSON follows MongoDB Extended JSON syntax correctly.
  • Connection errors: If the MongoDB connection fails or the specified database/collection does not exist, the node will report connection or database existence errors. Verify credentials and database names.
  • Empty or invalid collection name: The collection property is required; leaving it empty will cause errors.
  • Insert failures: If the data violates MongoDB schema constraints or indexes, insertion may fail. Check MongoDB server logs and data validity.
  • Output confusion: If Lean Output is false, the output may contain MongoDB ObjectId objects instead of strings, which might be unexpected in some workflows.

Links and References

Discussion