MongoDBNg icon

MongoDBNg

Nextgen Find, insert and update documents in MongoDB

Overview

This node provides advanced MongoDB operations for finding, inserting, and updating documents in a MongoDB database. It is designed to work with the latest MongoDB driver and supports common database operations such as update, insert, find, aggregate, delete, and specialized update operations like findOneAndReplace and findOneAndUpdate.

Typical use cases include:

  • Updating existing documents based on a key field.
  • Inserting new documents into a specified collection.
  • Performing complex queries or aggregations.
  • Replacing or updating single documents atomically.
  • Deleting multiple documents matching a query.

For example, you can update user profiles by specifying the user ID as the update key, or insert multiple new product records into a products collection.

Properties

Name Meaning
Collection The MongoDB collection where the operation will be performed.
Update Key The property name used to identify which documents to update (e.g., "id" or "_id").
Fields Comma-separated list of fields to include in the new document when updating or inserting.
Upsert Boolean flag indicating whether to insert a new document if no matching document is found.
Options Additional options controlling behavior:
- Date Fields Comma-separated list of fields that should be parsed as MongoDB Date type.
- Use Dot Notation Whether to use dot notation to access nested date fields.
- _id as ObjectId Whether to automatically convert the _id field to a MongoDB ObjectId type (default true).

Output

The output is an array of JSON objects representing the results of the executed operation:

  • For update and similar operations (findOneAndReplace, findOneAndUpdate), the output contains the updated documents after applying changes.
  • For insert, the output includes the inserted documents augmented with their generated IDs.
  • For delete, the output contains an object with the count of deleted documents.
  • For find and aggregate, the output contains the documents returned by the query or aggregation pipeline.

If errors occur during processing and the node is configured to continue on failure, the output items will contain an error property describing the issue.

Binary data is not produced by this node.

Dependencies

  • Requires a MongoDB database connection with appropriate credentials.
  • Needs the MongoDB Node.js driver to connect and perform operations.
  • Requires n8n credentials configured with a valid MongoDB connection string or parameters.
  • No additional external services are required.

Troubleshooting

  • Common issues:

    • Invalid MongoDB connection string or incorrect database name will cause connection failures.
    • Malformed JSON in query or sort parameters will throw parsing errors.
    • Using an invalid update key or missing required fields may result in no documents being updated.
    • Attempting to convert invalid strings to ObjectId will cause errors unless disabled.
  • Error messages:

    • "Database \"<name>\" does not exist": Check that the database name is correct and accessible.
    • JSON parse errors for query or sort: Ensure all JSON inputs are properly formatted.
    • MongoDB operation errors (e.g., duplicate keys, validation errors): Review the document structure and constraints.
  • To handle errors gracefully, enable the node's "Continue On Fail" option to process remaining items even if some fail.

Links and References

Discussion