MongoDB icon

MongoDB

Find, insert and update documents in MongoDB

Overview

This node allows you to update documents in a MongoDB collection. It is useful when you want to modify existing records based on a specific key, optionally inserting new documents if no match is found (upsert). Common scenarios include updating user profiles, product details, or any data stored in MongoDB where you need to selectively change fields.

For example, you can update a user's email address by specifying the user ID as the update key and providing the new email field. If the user does not exist and upsert is enabled, a new document will be created.

Properties

Name Meaning
Collection The name of the MongoDB collection where documents will be updated.
Update Key The property name used to identify which documents to update (e.g., "id").
Fields Comma-separated list of fields to include in the updated document.
Upsert Whether to insert a new document if no matching document is found (true or false).
Options Additional options:
- ID Fields Comma-separated list of fields that should be parsed as Mongo ObjectId type.
- Date Fields Comma-separated list of fields that should be parsed as Mongo Date type.
- Use Dot Notation Whether to use dot notation to access nested date fields (true or false).

Output

The output consists of an array of JSON objects representing the updated documents. Each item corresponds to one input item processed by the node. If an error occurs during processing of an item and the node is configured to continue on failure, the output for that item will contain an error field with the error message.

The output JSON structure includes all fields specified in the Fields property, with appropriate conversions applied (e.g., ObjectId and Date types). No binary data is produced by this operation.

Dependencies

  • Requires a MongoDB database connection.
  • Needs an API key credential or connection string configured in n8n to authenticate with MongoDB.
  • Uses the official MongoDB Node.js driver internally.

Troubleshooting

  • Common issues:

    • Incorrect collection name or database name may cause errors indicating the collection does not exist.
    • Invalid ObjectId format in the update key field can cause failures.
    • Improperly formatted JSON in the Fields or other parameters may lead to parsing errors.
    • Missing required credentials or incorrect connection string will prevent connection.
  • Error messages and resolutions:

    • "Database \"<name>\" does not exist": Verify the database name in your credentials.
    • "Invalid ObjectId": Ensure that fields marked as ObjectId contain valid hexadecimal strings.
    • "Failed to parse JSON": Check that all JSON inputs (like query or projection) are correctly formatted.
    • Connection errors: Confirm network access to MongoDB and correctness of authentication details.

Links and References

Discussion