Overview
This node allows you to interact with a MongoDB database by performing various operations such as finding, inserting, updating, deleting, and aggregating documents within specified collections. The Insert operation specifically enables adding new documents into a chosen MongoDB collection.
Typical use cases include:
- Adding new records to a database from incoming workflow data.
- Automating data entry processes where structured data needs to be stored in MongoDB.
- Integrating MongoDB insertions as part of larger automation workflows, e.g., logging events or user actions.
For example, you might use this node to insert customer information collected from a form directly into a MongoDB collection named "customers".
Properties
Name | Meaning |
---|---|
Collection | The name of the MongoDB collection where the new document(s) will be inserted. |
Fields | Comma-separated list of fields to include in the new document(s). These specify which input data fields to map into the MongoDB document. |
Options | Additional options for insertion: - ID Fields: Comma-separated list of fields parsed as Mongo ObjectId type. - Date Fields: Comma-separated list of fields parsed as Mongo Date type. - Use Dot Notation: Boolean flag indicating whether to use dot notation to access date fields. |
Output
The output is an array of JSON objects representing the inserted documents. Each object corresponds to one inserted document and includes all fields that were inserted plus an additional id
field containing the MongoDB-generated identifier for that document.
Example output item:
{
"field1": "value1",
"field2": "value2",
"id": "ObjectId('...')"
}
No binary data output is produced by this operation.
Dependencies
- Requires a MongoDB database connection configured via credentials that provide necessary authentication (e.g., connection string or parameters).
- The node depends on the official MongoDB Node.js driver to perform database operations.
- Proper permissions are needed on the MongoDB server to insert documents into the specified collection.
Troubleshooting
Common issues:
- Invalid or missing collection name will cause errors.
- Incorrectly formatted fields or missing required fields can lead to insertion failures.
- If ID fields or date fields are not properly specified, the node may fail to convert them correctly, causing errors.
- Connection issues due to invalid credentials or network problems.
Error messages:
- Errors related to MongoDB connection failure usually indicate credential or network configuration problems.
- Insert operation errors often mention validation issues or duplicate key errors if unique constraints are violated.
Resolutions:
- Verify the collection name exists and is spelled correctly.
- Ensure the input data contains all required fields and matches expected types.
- Confirm that ID and date fields are correctly listed if used.
- Check MongoDB credentials and network connectivity.
- Use the node’s "Continue On Fail" option to handle errors gracefully during batch inserts.
Links and References
- MongoDB Official Documentation
- MongoDB Node.js Driver API
- n8n Documentation - MongoDB Node (for general usage guidance)