Skip to main content

MongoDB Overview

The MONGODB route stores MQTT messages as documents in MongoDB collections. It uses the CLEAN query format for simplified JSON syntax and supports flexible schemas for IoT data.
MongoDB is ideal for IoT data with varying schemas. Store sensor readings, events, and logs without defining rigid table structures upfront.

Basic Syntax


Connection Configuration

MONGODB_CONFIG Parameters

CONNECTION_STRING
string
required
MongoDB connection URI. Format: mongodb://user:password@host:port/database?options
DATABASE
string
Target database name (can also be specified in connection string).
Never hardcode credentials in production. Use environment variables and encrypted secrets to keep sensitive values out of your route definitions:
See Environment Variables & Secrets for setup and usage.

CLEAN Query Format

MongoDB routes use the CLEAN format for simplified document insertion:

CLEAN Placeholders


Event Configuration

EVENT Parameters

SOURCE_TOPIC
string
required
MQTT topic pattern that triggers the INSERT. Supports wildcards.
QUERY
string
required
CLEAN format query for document insertion.
DESTINATION_TOPIC
string
MQTT topic to publish operation status.

Writing Data

Store sensor readings as documents:
Alternative: STORE IN with Models — Instead of writing EVENT queries, you can bind a model directly to this route. Every PUBLISH MODEL call automatically inserts a row — no query needed:
See Data Storage Overview for the full STORE IN workflow.

Reading Data

EVENTs also support read queries to retrieve data from the database. Publish a message to the event’s SOURCE_TOPIC, and the query result is published to DESTINATION_TOPIC. MongoDB read queries use the CLEAN format with filter, sort, and limit instead of SQL:
To trigger this query, publish the sensor ID to the source topic:
The matching document is published to db/result/latest, where your actions or external clients can consume it.

CLEAN Read Operators


Connection String Examples


Troubleshooting

  • Verify CONNECTION_STRING is correct
  • Check MongoDB is running and accessible
  • Verify firewall allows connections on port 27017
  • For Atlas, ensure IP whitelist includes your server
  • Verify username and password in connection string
  • Check authSource parameter matches authentication database
  • Verify user has permissions on target database
  • Verify collection name is valid
  • Check placeholder names match topic structure
  • Ensure document structure is valid JSON

Next Steps

Data Storage Routes Overview

Compare all storage options.

OpenSearch Route

Configure OpenSearch for search and analytics.
Last modified on May 22, 2026