Using MongoDB With Node-RED (2026 Updated)

This guide provides implementation procedures for integrating MongoDB with Node-RED. It covers configuration requirements, operational patterns, and a complete implementation example using a customer relationship management system.

Understanding MongoDB

MongoDB is an open-source NoSQL database that stores data in flexible, JSON-like documents rather than rigid table structures. Each document can maintain its own schema, which allows for data model evolution without requiring database-wide migrations. This architectural approach suits applications where data structures change frequently or vary between records.

The database uses a distributed architecture that supports horizontal scaling across multiple nodes. It handles high-volume workloads and provides query performance suitable for real-time operations.

Data Organization

MongoDB structures data into three hierarchical components, which differ from traditional relational databases:

  • Collections replace tables
  • Documents replace rows
  • Fields replace columns

"Annotomy of MongoDB document"

Technical Considerations

Several factors make MongoDB appropriate for Node-RED integration:

MongoDB's document model aligns directly with JSON data structures, which eliminates transformation overhead between Node-RED message objects and database records. The schema-less architecture supports rapid iteration without requiring schema migrations for each data model change.

The database scales horizontally by distributing data across multiple servers. While some SQL databases support horizontal scaling, MongoDB's architecture implements this pattern natively. The query language uses a document-based syntax that matches common programming patterns.

MongoDB Atlas provides managed database services with automated backups, security controls, and monitoring tools. The platform includes specialized implementations for industrial applications. Additional information is available at MongoDB Atlas for Manufacturing and Automotive.

Configuration Procedures

Installing the MongoDB Node

  1. Open Node-RED Settings (top-right menu)
  2. Select "Manage Palette"
  3. Navigate to the "Install" tab
  4. Search for node-red-contrib-mongodb4
  5. Install the package

Connection Parameters

Collect the following configuration values before proceeding:

  • Host: Server IP address or hostname
  • Port: Connection port (default: 27017; may not be required for managed services)
  • Database: Target database name
  • User: Account username with appropriate database privileges
  • Password: Account password

For TLS/SSL configuration and other advanced options, consult the node documentation.

Environment Variable Configuration

Store connection credentials in environment variables rather than embedding them directly in flows. This prevents credential exposure in version control and exported flow definitions. Reference the guide Using Environment Variables in Node-RED for additional context.

"Screenshot displaying FlowFuse instance settings"

  1. Navigate to instance settings
  2. Select the "Environment" tab
  3. Add variables for each configuration parameter
  4. Save the configuration
  5. Restart the instance using the Actions menu

Configuring the MongoDB Node

Configure the node to use the environment variables:

  1. Add a MongoDB4 node to the canvas
  2. Open the node configuration
  3. Click the edit icon next to the connection field
  4. Reference environment variables as shown below

"Screenshot displaying connection configuration of MongoDB 4 node."

Implementation Example: Customer Management System

This section demonstrates MongoDB operations through a functional customer relationship management system. The implementation covers create, read, update, and delete operations using a representative data structure.

Data Schema

The customer records use the following structure:

{
"_id": "NXaxeFEK",
"firstname": "alice",
"lastname": "demo",
"email": "userdemo601@gmail.com",
"phone": "+19876543561",
"company": "self",
"status": "Prospect",
"source": "website"
}

MongoDB Operations

The implementation uses five core operations:

  • InsertOne: Adds a single document to a collection
  • Find: Retrieves documents matching specified criteria
  • UpdateOne: Modifies a single document based on query parameters
  • DeleteOne: Removes a single document matching query criteria
  • Drop: Deletes an entire collection

Refer to the MongoDB CRUD documentation for the complete operation set.

Additional Dependencies

NanoID Generator

Install node-red-contrib-friendly-id through the palette manager. This package generates compact, URL-safe unique identifiers for customer records. The implementation uses NanoID instead of manual ID entry or sequential numbering.

Dashboard Interface

The example uses Node-RED Dashboard 2.0 for the user interface. Follow the Dashboard setup instructions to install and configure the dashboard nodes.

Creating Customer Records

  1. Add a ui-form widget to the canvas
  2. Configure form elements for firstname, lastname, email, phone, company, status, and source

"Screenshot displaying form widget configuration to insert data in MongoDB"

  1. Add a friendly-id node
  2. Configure it to generate a random ID with your preferred length
  3. Set output destination to msg.payload._id

"Screenshot displaying friend-id node configuration"

  1. Add a change node
  2. Set msg.payload to [msg.payload] using JSONata expression type
  3. This wraps the payload in an array as required by the insertOne operation

"Screenshot displaying change node setting payload containing data that needs to be inserted in the database."

  1. Configure the MongoDB4 node:
    • Select the previously configured connection
    • Set collection name to "customers"
    • Set operation to "insertOne"

"Screenshot displaying configuration of MongoDB 4 node for inserting data"

  1. Wire the nodes as shown:

"Screenshot displaying connections of wires in the 'Insert Data into Database' flow"

Retrieving Customer Records

  1. Add an inject node
  2. Configure it to send an empty object {}
  3. Set the node to repeat at your preferred interval for automatic table updates
  4. Add a MongoDB4 node
  5. Select your connection and set operation to "find"

"Screenshot displaying configuration of MongoDB 4 node for retrieving data"

  1. Add a ui-table widget
  2. Configure the maximum rows according to your requirements

"Screenshot displaying ui-table widget configuration"

  1. Wire the nodes as shown:

"Screenshot displaying connections of wires in the 'Retrive Data from Database' flow"

Updating Customer Records

  1. Add a ui-form widget with fields for "id" and "status"

"Screenshot displaying form widget configuration to update data in MongoDB"

  1. Add a change node
  2. Set msg.payload to the following JSON structure:
[
{ "_id": msg.payload._id },
{ "$set": { "status": msg.payload.status } }
]

The first object specifies the query criteria (which document to update). The second object defines the update operation using MongoDB's $set operator.

"Screenshot displaying the change node setting payload as an array containing a query and operation to perform an update operation in the database"

  1. Add a MongoDB4 node and set operation to "updateOne"

"Screenshot displaying configuration of MongoDB 4 node for updating data"

  1. Wire the nodes as shown:

"Screenshot displaying connections of wires in the 'Update Data from Database' flow"

Deleting Customer Records

  1. Add a ui-form widget with fields for "id" and "firstname"

"Screenshot displaying form widget configuration to delete data in MongoDB"

  1. Add a change node
  2. Set msg.payload to the following structure:
[
{
"_id": msg.payload._id,
"firstname": msg.payload.firstname
},
{
"$delete": ""
}
]

The query includes both ID and firstname to provide additional verification before deletion.

"Screenshot displaying the change node setting payload as an array containing a query and operation to perform an delete operation in the database"

  1. Add a MongoDB4 node and set operation to "deleteOne"

"Screenshot displaying configuration of MongoDB 4 node for deleting data"

  1. Wire the nodes as shown:

"Screenshot displaying connections of wires in the 'Delete Data from Database' flow"

Removing Collections

  1. Add an inject node configured to send an empty object
  2. Add a MongoDB4 node
  3. Specify the collection name to remove
  4. Set operation to "drop"

"Screenshot displaying configuration of MongoDB4 node for droping collection from database"

  1. Wire the nodes as shown:

"Screenshot displaying connections of wires in the 'Drop collecton from Database' flow"

Verification and Troubleshooting

Connect a debug node to the output of any MongoDB4 node to monitor operation results and diagnose errors. The following examples show successful operation responses:

Operation Response Messages

// Insert operation
{
"acknowledged": true,
"insertedId": "BKoIzMuW"
}

// Update operation
{
"acknowledged": true,
"modifiedCount": 1,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1
}

// Delete operation
{
"acknowledged": true,
"deletedCount": 1
}

// Drop operation returns boolean true

Deployment

"Screenshot displaying flow of CRM System"

  1. Deploy the flow using the Deploy button
  2. Open the dashboard using the button in the Dashboard 2.0 sidebar
  3. Use inject nodes for retrieval and drop operations
  4. Use the forms for create, update, and delete operations

"Screenshot displaying dashboard view of CRM System"