Skip to main content

MongoDB Overview

The MONGODB connector inserts MQTT messages into MongoDB collections as documents and reads them back. Each event either inserts one document or runs a find, and models can store into a collection with STORE IN.
MongoDB suits IoT data whose shape changes over time. Store sensor readings, events, and logs without defining a table first.

Basic Syntax


Connection Configuration

MONGODB_CONFIG Parameters

string
required
MongoDB URI: mongodb://host:port/?options or mongodb+srv://host/?options. Replica sets, TLS, timeouts, and pool size are set here as URI options. The URI is checked at deploy: one that cannot be read is refused, and the error never repeats the URI.
string
required
Database that the events and STORE IN use.
string
Login given apart from the URI, so the password needs no percent-encoding. It takes precedence over a login in the URI. It authenticates exactly as a login in the URI would, so authSource, authMechanism, and authMechanismProperties from the URI still apply.
string
Password for USERNAME. Use GET SECRET.
Never hardcode credentials in production. Keep the URI or the password in an encrypted secret:
See Environment Variables & Secrets for setup and usage.

Credentials in the URI

A user name or password inside the URI must be percent-encoded: @ : / ? # [ ] % become %40 %3A %2F %3F %23 %5B %5D %25. A URI with an unencoded @ in the password is refused at deploy. To avoid encoding, leave the login out of the URI:

URI options and defaults

The connector applies these defaults only when the URI does not set the option itself: Every other option, such as retryWrites, replicaSet, or tls, is used as written in the URI.

TLS

Set TLS in the URI with tls=true. Add tlsCAFile=/path/ca.pem for a private certificate authority. tlsInsecure=true skips certificate checks; use it only in a test setup with a self-signed certificate.

Event Configuration

EVENT Parameters

string
MQTT topic pattern that triggers the event. Supports wildcards. Use SOURCE_TOPIC or EVERY.
string
required
An insert or a find, as a CLEAN or JSON object. See Query format.
string
MQTT topic where the result is published. A + in it takes the matching level of the source topic. Required for an EVERY event.
string
Run the event on a schedule, for example WITH EVERY 10 SECONDS.

Query format

QUERY is a JSON object, or the CLEAN form (CLEAN:{key: value} with unquoted keys and values). CLEAN is the practical form inside a LoT string. Extended JSON such as {"$oid": "…"} and {"$date": "…"} works in filters and documents. A query with a document key inserts. Any other query is a find. Update, delete, and aggregation are not supported.

Find options

Placeholders

Each placeholder goes in as one JSON value, so payload text is stored as sent:
  • A JSON number, true, false, or null keeps its type. Text that reads as a number or as true / false is stored as one.
  • Any other text is stored as a string, with its quotes, apostrophes, commas, semicolons, and braces. Dates keep their offset.
  • {value.json}, or a {payload.json.key} that holds an object or an array, goes in as that JSON.
  • Inside a quoted string ("note: {payload}"), the value is part of that string.
A payload value that is a JSON object goes into a filter as an object, so it can carry query operators such as {"$ne": null}. Do not build a find filter from payloads you do not trust. Keep untrusted publishers off the event’s source topic with a rule.

Writing Data

Store each JSON payload with its topic:
Each insert publishes a result to DESTINATION_TOPIC:
Alternative: STORE IN with Models. Instead of writing an insert query, bind a model to this connector. Each PUBLISH MODEL inserts one document with the model’s fields and their JSON types, plus a timestamp date unless the model has a timestamp field. MongoDB creates the collection on first use:
See Data Storage Overview for the full STORE IN workflow.

Reading Data

A find event reads documents on demand. Publish to its SOURCE_TOPIC, and the matching documents are published to DESTINATION_TOPIC as one JSON array:
To run it, publish the sensor ID to the source topic:
The result is relaxed Extended JSON. Numbers and text appear as they are, _id as {"$oid": "…"}, dates as {"$date": "…"}, and decimals as {"$numberDecimal": "…"}:

Timed Polling

An EVERY event runs the same find on a schedule:

When an event fails

A failed insert or find publishes nothing. The error is logged, and the event’s ON ERROR block runs. A query the server refuses leaves the connector connected. Only a connection failure marks it disconnected.

While MongoDB is unreachable

After a connection failure, the next event or STORE IN checks MongoDB at once. If MongoDB answers, the work runs. If not, the work is skipped and later checks run at most every 5 seconds. Work that arrives between checks is skipped instead of waiting out the server-selection timeout. Each check logs how many events and STORE IN documents were skipped: a warning while MongoDB is still down, and an information line with the total when the connector reconnects. A removed or redeployed connector closes its connections. Another connector with the same URI keeps working.

Checking the connection

-checkConnectorConnection <Name> pings the database. The reply names the database and the hosts and ports it tried, never the login or the password. The check waits at most 10 seconds.
A failed check says what to look at: authentication, TLS, a refused connection, an unknown host, or no answer within the server-selection timeout.

Connection String Examples


Troubleshooting

  • Start the URI with mongodb:// or mongodb+srv://
  • Percent-encode @ : / ? # [ ] % in the user name and password, or move the login to USERNAME and PASSWORD
  • Check that MongoDB is running and listening on that host and port
  • Check that a firewall allows the connection (port 27017 by default)
  • For Atlas, add the broker’s address to the IP access list
  • Check the user name and password
  • Check that authSource names the authentication database
  • Check that the user has rights on the target database
  • Name the collection in every query
  • Use only collection, filter, projection, sort, skip, and limit in a find
  • Write filter, and the object forms of projection and sort, as JSON objects
  • Add a DESTINATION_TOPIC to see the result

Next Steps

Data Storage Connectors Overview

Compare all storage options.

OpenSearch Connector

Configure OpenSearch for search and analytics.
Last modified on December 4, 2026