Skip to main content
Version: Pro Edition for Eclipse Mosquitto 3.1

mongodb-bridge-examples

An example for the plugin configuration (mongodb-bridge.json) is:

{
"connections": [
{
"name": "connection-to-db1",
"connection": {
"connectionParameters": {
"hostname": "mongodb",
"port": 27017,
"database": "db1",
"credentials": {
"username": "user1",
"password": "secret123"
}
}
},
"options": {
"maxQueuedMessages": 100000,
"retryInsertMinDelay": 5,
"retryInsertMaxDelay": 25000
},
"topicMappings": [
{
"name": "topic-mapping",
"target": "sensorData",
"schemaMapping": "reduced-mapping",
"mqttTopics": [
"sensor_data/#"
]
}
]
}
],
"schemaMappings": [
{
"name": "reduced-mapping",
"mapping": [
{
"source": "payload",
"target": "data"
},
{
"source": "hostname",
"target": "nodeId"
}
]
}
]
}

With this example the plugin will create a single client to connect to a MongoDB instance with the URI: mongodb://user1:secret123@mongodb:27017.

All data received on sensor_data/# topics will be published to the collection sensorData of the database db1. This is configured using topic mappings, which define the MQTT topics inserted to MongoDB. Without a topic mapping, no messages will be written to MongoDB. Each topic mapping defines a list of MQTT topic filters, and the MongoDB collectio nwhere matching messages will be written.

A topic mapping can also reference a custom schema, or use the default schema. In the example above the data is reduced to:

  • payload of the MQTT message stored in a data column of the collection
  • hostname stored into the nodeId column of the collection

Instead of using the default schema mapping containing all information of the default schema.

info

This is an example configuration snippet, which applies to the docker container setup. For installation not running in a container the above configuration needs to be adjusted accordingly.

persistence_location is used as the search path for the plugins' config file.

In case the plugin should connect to a MongoDB Atlas cluster or custom options should be used the original connection string can be provided as follows:

{
"connections": [
{
"name": "connection-to-db-cluster",
"connection": {
"connectionURI": "mongodb+srv://user1:secret123@mongodb/db_name"
},
"options": {
"maxQueuedMessages": 100000
},
"topicMappings": [
{
"name": "topic-mapping",
"target": "sensorData",
"schemaMapping": "reduced-mapping",
"mqttTopics": [
"sensor_data/#"
]
}
]
}
],
"schemaMappings": [
{
"name": "reduced-mapping",
"mapping": [
{
"source": "payload",
"target": "data"
},
{
"source": "hostname",
"target": "nodeId"
}
]
}
]
}

In this example the plugin takes the connection string as a whole. The plugin will automatically resolve the host from the connection string provided as the connectionURI parameter and parse the parameters hostname, port, database, credentials. Therefore, those have to be omitted in the config. The passwordin the connection string always has to be URL encoded (percent-encoded). Additionally, standard MongoDB connection strings can also be provided as in the previous snippet.