Overview
This node performs various MongoDB operations on specified collections within a database. It is useful for automating interactions with MongoDB databases directly from n8n workflows, such as inserting new documents, updating existing ones, querying data, aggregating results, replacing documents, or deleting entries.
For the Insert operation specifically, the node inserts one or more JSON documents into a specified MongoDB collection. This is beneficial when you want to programmatically add data records to your database, for example, logging user activity, storing form submissions, or syncing data from other sources.
Properties
| Name | Meaning |
|---|---|
| Collection | The name of the MongoDB collection where the document(s) will be inserted. |
| Document (JSON Format) | The JSON-formatted document data to insert into the collection. Multiple documents can be inserted in one execution by providing multiple input items. |
Output
The output is an array of JSON objects, each representing one inserted document. Each output item contains the original document fields plus an additional _id field which holds the unique identifier assigned by MongoDB upon insertion.
Example output item:
{
"field1": "value1",
"field2": "value2",
"_id": "ObjectId('...')"
}
No binary data output is produced by this operation.
Dependencies
- Requires a valid connection to a MongoDB database.
- Needs credentials that provide access to the MongoDB instance (such as a connection string and database name).
- The node uses the official MongoDB Node.js driver internally to perform operations.
Troubleshooting
Common issues:
- Invalid JSON format in the "Document" property will cause parsing errors.
- Incorrect collection names or missing collections will result in operation failures.
- Connection issues due to invalid credentials or network problems.
Error messages:
- Parsing errors when the document JSON is malformed. Solution: Ensure the JSON syntax is correct.
- Authentication or connection errors. Solution: Verify the MongoDB credentials and network accessibility.
- Insert operation errors if the document violates schema constraints or duplicate key errors. Solution: Check MongoDB collection schema and indexes.
If the node is set to continue on failure, errors will be returned as JSON objects with an error field describing the issue.