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

DEFINE ROUTE SensorDB WITH TYPE MONGODB
    ADD MONGODB_CONFIG
        WITH CONNECTION_STRING "mongodb://user:[email protected]:27017/iot?authSource=admin"
        WITH DATABASE "iot"
    ADD EVENT StoreSensorReading
        WITH SOURCE_TOPIC "sensors/+/reading"
        WITH QUERY "CLEAN:{collection: readings, document: {sensor_id: {sensor_id}, value: {value.json}, timestamp: {timestamp}}}"

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).

CLEAN Query Format

MongoDB routes use the CLEAN format for simplified document insertion:
WITH QUERY "CLEAN:{collection: <name>, document: {<field>: <value>, ...}}"

CLEAN Placeholders

PlaceholderDescription
{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

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

Complete Examples

Store sensor readings as documents:
DEFINE ROUTE SensorStorage WITH TYPE MONGODB
    ADD MONGODB_CONFIG
        WITH CONNECTION_STRING "mongodb://iot_user:[email protected]:27017/iot?authSource=admin"
        WITH DATABASE "iot"
    ADD EVENT StoreReading
        WITH SOURCE_TOPIC "sensors/+/reading"
        WITH DESTINATION_TOPIC "db/mongo/status"
        WITH QUERY "CLEAN:{collection: readings, document: {timestamp: {timestamp}, topic: {source_topic}, data: {value.json}}}"

Connection String Examples

ScenarioConnection String
Localmongodb://localhost:27017/iot
Authenticatedmongodb://user:password@host:27017/iot?authSource=admin
Replica Setmongodb://host1:27017,host2:27017/iot?replicaSet=rs0
Atlasmongodb+srv://user:[email protected]/iot

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