MongoDB Overview
TheMONGODB 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
MongoDB connection URI. Format:
mongodb://user:password@host:port/database?optionsOptional Settings
Optional Settings
Target database name (can also be specified in connection string).
CLEAN Query Format
MongoDB routes use the CLEAN format for simplified document insertion:CLEAN Placeholders
| Placeholder | Description |
|---|---|
{value.json} | Full JSON payload as embedded document |
{value.json.field} | Specific field from JSON payload |
{timestamp} | Message timestamp |
{source_topic} | Original MQTT topic |
{field} | Field extracted from topic path |
Event Configuration
EVENT Parameters
MQTT topic pattern that triggers the INSERT. Supports wildcards.
CLEAN format query for document insertion.
Optional Settings
Optional Settings
MQTT topic to publish operation status.
Writing Data
- Basic Document Storage
- Structured Documents
- Multiple Collections
- Replica Set
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 See Data Storage Overview for the full STORE IN workflow.
PUBLISH MODEL call automatically inserts a row — no query needed:Reading Data
EVENTs also support read queries to retrieve data from the database. Publish a message to the event’sSOURCE_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:
db/result/latest, where your actions or external clients can consume it.
CLEAN Read Operators
| SQL Equivalent | MongoDB CLEAN Syntax |
|---|---|
SELECT * | {} |
WHERE a = 1 | filter: "{\"a\": 1}" |
WHERE a > 10 | filter: "{\"a\": {\"$gt\": 10}}" |
ORDER BY a DESC | sort: "a desc" |
LIMIT 10 | limit: 10 |
SELECT a, b | projection: "a, b" |
Connection String Examples
| Scenario | Connection String |
|---|---|
| Local | mongodb://localhost:27017/iot |
| Authenticated | mongodb://user:password@host:27017/iot?authSource=admin |
| Replica Set | mongodb://host1:27017,host2:27017/iot?replicaSet=rs0 |
| Atlas | mongodb+srv://user:password@cluster.mongodb.net/iot |
Troubleshooting
Connection Failed
Connection Failed
- 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
Authentication Failed
Authentication Failed
- Verify username and password in connection string
- Check authSource parameter matches authentication database
- Verify user has permissions on target database
CLEAN Query Errors
CLEAN Query Errors
- 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.

