Mongo Db Operations icon

Mongo Db Operations

MongoDB Operations

Update

AI-generated

Overview

This node performs various MongoDB operations on documents within specified collections. It connects to a MongoDB database and executes commands such as updating, finding, aggregating, inserting, replacing, or deleting documents based on user-defined queries and parameters.

The Update operation specifically updates a single document in a collection matching a given query with the provided update instructions and optional update options.

Common Use Cases

  • Updating user profiles or records in a MongoDB collection based on specific criteria.
  • Modifying fields of documents dynamically during workflow execution.
  • Automating data maintenance tasks like setting flags, timestamps, or correcting values in documents.

Practical Example

You want to update the birthdate field for users born after 1950-01-01:

  • Collection: users
  • Query: { "birth": { "$gt": "1950-01-01" } }
  • Update: { "$set": { "birth": "1960-01-01" } }

This will find one document matching the query and apply the update operation.


Properties

Name Meaning
Collection The name of the MongoDB collection where the update operation will be performed.
Query (JSON Format) A JSON-formatted MongoDB find query that specifies which document to update.
Update A JSON object defining the update operations to apply to the matched document(s).
Update Options Optional JSON object specifying additional update options (e.g., upsert, arrayFilters).

Output

  • The output is an array of JSON objects representing the input items passed through unchanged.
  • No direct result from the update operation is returned in the output JSON; it simply passes the original input data forward.
  • There is no binary data output.

Dependencies

  • Requires a valid connection to a MongoDB database via a connection string and credentials.
  • The node depends on MongoDB client libraries to connect and perform operations.
  • Proper configuration of MongoDB credentials (such as an API key or connection string) is necessary in n8n.

Troubleshooting

  • Common Issues:

    • Malformed JSON in the Query, Update, or Update Options fields can cause parsing errors.
    • Incorrect collection names or missing collections will lead to runtime errors.
    • Insufficient permissions or invalid credentials will prevent successful connection.
    • Using update operators incorrectly (e.g., missing $set) may result in unexpected behavior.
  • Error Messages:

    • JSON parse errors: Ensure all JSON inputs are valid and properly formatted.
    • Connection errors: Verify MongoDB credentials and network accessibility.
    • MongoDB operation errors: Check query correctness and update syntax.
  • Resolution Tips:

    • Validate JSON inputs using online tools before pasting.
    • Confirm the MongoDB URI and credentials are correct and have required privileges.
    • Review MongoDB documentation for proper update operator usage.

Links and References

Discussion