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 complex queries and updates using MongoDB's rich data types. The "Find And Update" operation specifically allows you to find documents in a specified collection based on a key or query and update them accordingly. It supports upserting (inserting if no match is found), returning the document before or after the update, and controlling output formatting.

Common scenarios include:

  • Updating user records by their unique identifier.
  • Modifying status fields of documents matching certain criteria.
  • Performing atomic find-and-update operations with control over returned data.
  • Using EJSON format to handle MongoDB-specific data types like ObjectId, Date, Decimal128, etc.

Practical example:

  • You have a collection of orders and want to update the status of an order identified by its _id to "shipped" while also recording the shipping date.

Properties

Name Meaning
Collection The MongoDB collection where the documents reside.
Update Key The property name used to identify which documents to update, typically _id.
Update Query (EJSON Format) A MongoDB query in EJSON format to select documents to update (used in "update" and "findOneAndUpdate" operations).
Update Data (EJSON Format) The update operations in EJSON format specifying how to modify the matched documents (required).
Upsert Whether to insert a new document if no existing document matches the query/key.
Lean Output If true, returns plain JavaScript objects without MongoDB-specific ObjectId wrappers; otherwise, returns full MongoDB objects.
Options Additional options for the update operation:
- Return Updated Document For "findOneAndUpdate" and "findOneAndReplace" operations, choose whether to return the document before or after the update. Options: "Before", "After".

Output

The node outputs an array of items, each containing a json field representing the result of the update operation:

  • For bulk updates (update), it includes counts such as matchedCount, modifiedCount, upsertedCount, and possibly upsertedId.
  • For findOneAndUpdate and findOneAndReplace, it returns the document either before or after the update depending on the option selected.
  • The output respects the "Lean Output" setting, returning either plain objects or MongoDB objects.
  • In case of errors per item, the output contains an error field describing the issue.

No binary data output is produced by this operation.

Dependencies

  • Requires a MongoDB database connection configured via credentials that provide necessary authentication (e.g., API key or connection string).
  • Uses MongoDB Node.js driver and BSON library for EJSON parsing and handling.
  • The node expects valid EJSON formatted strings for queries and update data.
  • No additional external services are required beyond MongoDB.

Troubleshooting

  • Invalid EJSON query or update data: Errors like Invalid EJSON query indicate malformed JSON or unsupported EJSON constructs. Ensure your input strictly follows MongoDB's EJSON format.
  • Database or collection not found: Connection errors or warnings about missing databases/collections suggest misconfiguration of credentials or typos in collection names.
  • Update key issues: Using an incorrect or missing update key can cause no documents to be matched or updated. Usually, _id is recommended.
  • Upsert behavior unexpected: If no documents are updated and no new document is inserted, verify the Upsert flag is enabled.
  • Return document option confusion: When using findOneAndUpdate or findOneAndReplace, ensure you understand whether you want the document before or after the update, controlled by the Return Updated Document option.
  • Connection closure: The node closes the MongoDB client connection after execution; long-running operations should be handled accordingly.

Links and References

Discussion