Explainer
Why structured text cannot write to MongoDB directly
Short answer
Structured text defines no HTTP, MQTT or database interface, so a PLC cannot open a MongoDB connection. Write documents through a server application that reads the PLC, an HTTP ingestion service the PLC posts to, or an MQTT broker with a database-writing consumer; pick the collector unless the PLC owns a documented client library.

On this page
- Why can't structured text open a MongoDB connection?
- Which of the three boundaries should carry the data?
- Is an MQTT publish the same as calling a REST endpoint?
- How should the ST client run without blocking the scan?
- How large must the send buffer be?
- What settings keep an outage from exhausting the PLC?
- Questions asked
Structured text has no database driver. IEC 61131-3 defines the language constructs, and no PLC runtime extends it with a MongoDB client: MongoDB speaks its own wire protocol over TCP, which no controller library implements for you. You get PLC values into MongoDB through one of three boundaries, and the decision is short: unless your controller ships a documented MQTT or HTTP client library, write a server application that reads the PLC and writes through a MongoDB driver.
Why can't structured text open a MongoDB connection? #
Structured text gives you variables, loops and function blocks, and nothing portable for networks. A MongoDB insert requires the database's wire protocol plus authentication over a TCP connection, and no IEC 61131-3 standard function block covers any part of that. What a PLC can do depends entirely on the manufacturer's runtime and the communications libraries installed with it, not on the language. So the honest question is never "how do I write a MongoDB function block in ST" but "which documented client library does my controller have, and what receives its traffic". The same limit applies to any SQL target, as covered in the comparison of PLC to SQL Server through REST or direct database access.
Which of the three boundaries should carry the data? #
Pick the server-side collector unless the PLC must initiate delivery and owns a supported client library. The three architectures differ in what runs where:
| Architecture | Request path | What the PLC needs |
|---|---|---|
| Server reads the PLC | Collector requests PLC values, writes via a MongoDB driver | A supported PLC access protocol only; no extra PLC code beyond exposing the data |
| PLC sends HTTP | PLC posts to an ingestion API; that service writes MongoDB | A documented HTTP client library with TLS support |
| PLC publishes MQTT | Broker receives the publish; a consumer writes MongoDB | A documented MQTT client library, a broker, and a consumer application |
The first path wins on the controller side because it needs no client code, no certificates and no PLC memory for buffers. The second and third are real options only where the vendor supplies the library: Siemens documents an LMQTT_Client function block in its LMQTT library for S7-1500 and S7-1200 CPUs, and ABB documents an MQTT library for AC500 V3 supporting BOOL, DINT, LREAL and STRING. If neither an HTTP nor an MQTT library exists for your controller, the decision is made: use the collector.
Is an MQTT publish the same as calling a REST endpoint? #
No, and neither one inserts a database document by itself. MQTT and HTTP are different application protocols: a publish reaches a broker and goes to subscribers, while an HTTP request reaches a web service. In both cases a separate piece of software, the ingestion service or the MQTT consumer, performs the MongoDB write. A server hosting MongoDB does not automatically expose an HTTP service; every listener is its own application with its own port, authentication and firewall rule. Before designing anything on the PLC side, get the ingestion service's method, path, authentication, content type and payload schema from its owner, and test it with a server-side client first. If your controller publishes over MQTT with TLS, the CPU clock and certificate trust must be right, the same failure mode as the S7-1500 MQTT TLS error 8089 clock and certificate check.
How should the ST client run without blocking the scan? #
Drive the client library from a scan-by-scan state machine, never a wait loop. A network transaction spans many task cycles: connect, negotiate TLS, send, receive, evaluate, and each of those steps completes asynchronously with the library's own done and error outputs. The pattern that works:
- Copy the measurement record into a send buffer once, so process updates cannot change the payload mid-request.
- Accept a new record only when the state machine is idle or the bounded queue has room.
- Apply a timeout to every pending step and release the connection on expiry.
- Expose state, last error, queue occupancy and last confirmed delivery to your HMI or SCADA so operators can see a stalled path before the queue fills.
If you drop to raw TCP sockets instead of a supplied library, you own HTTP framing, partial sends and partial receives yourself, because TCP is a byte stream and one send does not arrive as one receive. That work is exactly why structured text is rarely the right place for this layer, a point that runs through when to use structured text instead of ladder logic.
How large must the send buffer be? #
Size it from records per second, outage length and record size, and here is the arithmetic. Take a worked example: one plant PLC logs 2 records per second, each record serialized as 120 bytes of JSON, and you decide the queue must ride out a 10-minute network outage. Records at risk: 2 per second times 600 seconds = 1200 records. Buffer: 1200 records times 120 bytes = 144 000 bytes, about 141 kB. That fits an S7-1500 data block comfortably but not a small controller's work memory, which is a second reason small PLCs get the collector architecture. If your controller has 64 kB spare for buffers, the same math allows 64 000 divided by 120, about 533 records, which at 2 per second is only 266 seconds of ride-through; either shrink the record, lower the rate, or accept that records are dropped after that.
What settings keep an outage from exhausting the PLC? #
Set four limits before commissioning and leave them alone afterwards. Connection and response timeouts: 3 seconds each is a workable example for a local service; it keeps a dead server from pinning the state machine, and you confirm it against measured latency. Retry delay: 10 seconds between reconnect attempts, so a 5-hour outage causes about 1800 attempts instead of millions and does not hammer the recovering server. Queue occupancy limit: the buffer size from the previous section, with the policy that new records overwrite the oldest and raise an alarm at 90 percent occupancy. Delivery policy on timeout: discard the record after three failed attempts and count the loss, because a historian with a counted gap is honest and a blocked control task is not.
The decision, case by case: if the controller has a documented MQTT or HTTP client library and the plant requires PLC-initiated delivery, run the state-machine client against a broker or ingestion service with the limits above. If it has no such library, or its memory cannot hold the buffer your outage math demands, write the collector application, read the PLC with a protocol it already serves, and insert documents with a MongoDB driver on the server. Never implement the database protocol itself in structured text.
Questions asked
Can the PLC store MongoDB credentials if it writes through an HTTP service?
No. With an ingestion service the database credentials stay on the server, and the PLC carries only the credential the ingestion endpoint itself requires, such as an API token. That limits the damage if the controller's program is read, and lets you rotate the database password without touching the PLC.
How do I confirm the document that lands in MongoDB came from the right PLC value?
Send one known value, for example a counter reading of 847, through the whole path, then read the stored document back and compare the value, field name, timestamp and source identifier. If you use a server-side collector, the same test also proves the collector's PLC access method is configured for the right tag.
What happens to records the PLC cannot deliver during a network outage?
Whatever your delivery policy says, and you must choose one before commissioning: either the bounded queue fills and new records overwrite the oldest, or acquisition pauses. Decide how many records the queue holds from the buffer budget and the record size, and surface queue occupancy to the SCADA system so an approaching overflow raises an alarm.

