Read, query, and write MongoDB documents from a Dataset.
How-to → Examples → Feature → Application → DatasetDB
| Info |
|---|
This example shows how to read, aggregate, and write to a MongoDB collection using the Dataset Provider. Prerequisites:
|
| Table of Contents | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
This example walks through reading documents, running an aggregation pipeline, and inserting new records against a MongoDB collection. The work is done entirely from FrameworX, using the Dataset Provider surface of the MongoDB connector.
For other surfaces of the same connector (Device Protocol, UNS TagProvider, Historian StorageLocation), see the MongoDB Database Connector reference page.
The example creates one Dataset DB connection, three Queries, one Table, three Script Tasks, and a small set of UNS Tags. Each piece is configured in the Designer.
In Datasets / DBs, create a connection:
plant_dataMongoDB.Driverlocalhost27017plant_dataIn Datasets / Queries, create three queries against the plant_data connection.
QueryFindReadings. Returns the latest 10 readings for Plant01.
| Code Block | ||
|---|---|---|
| ||
{
"collection": "readings",
"filter": { "plant": "Plant01" },
"sort": { "ts": -1 },
"limit": 10
} |
QueryAggregateHourly. Hourly average of values for Plant01, expressed as an aggregation pipeline.
| Code Block | ||
|---|---|---|
| ||
[
{ "$match": { "plant": "Plant01" } },
{ "$group": { "_id": { "$dateTrunc": { "date": "$ts", "unit": "hour" } }, "avg": { "$avg": "$value" } } },
{ "$sort": { "_id": 1 } }
] |
QueryCount. Counts documents whose quality field is good.
| Code Block | ||
|---|---|---|
| ||
{
"count": "readings",
"filter": { "quality": "good" }
} |
In Datasets / Tables, create one table named TableReadings bound to the readings collection. A Dataset Table on a MongoDB collection uses the document _id as the primary key. Updates apply $set on the tracked columns and preserve any fields outside the tracked column list, matching the behavior of SQL Dataset Tables backed by a CommandBuilder.
In Unified Namespace / Tags, create the tags used by the tasks:
QueryResult. Receives the output of the Find and Aggregate queries.TriggerFind, TriggerAggregate, TriggerInsert. Script task triggers.LatestPlantCode, LatestValue. Values written back to MongoDB by the Insert task.In Scripts / Tasks, create three tasks, each triggered by the matching trigger tag.
Find task.
| Code Block | ||
|---|---|---|
| ||
@Tag.QueryResult = @Dataset.Query.QueryFindReadings.SelectCommand();
@Info.Trace("Find OK: " + @Tag.QueryResult); |
Aggregate task.
| Code Block | ||
|---|---|---|
| ||
@Tag.QueryResult = @Dataset.Query.QueryAggregateHourly.SelectCommand();
@Info.Trace("Aggregate OK: " + @Tag.QueryResult); |
Insert task (via the Dataset Table).
| Code Block | ||
|---|---|---|
| ||
@Dataset.Table.TableReadings.AddRow(); @Dataset.Table.TableReadings.Row["plant"] = @Tag.LatestPlantCode; @Dataset.Table.TableReadings.Row["value"] = @Tag.LatestValue; @Dataset.Table.TableReadings.Row["ts"] = @DateDateTime.NowUtcNow; int i = @Dataset.Table.TableReadings.UpdateCommand(Save(); @Info.Trace("Insert OK: " + i); |
Run After you finish the configuration and create the scripts, run the solution . Toggle each trigger tag to fire its corresponding task. The and trigger each task. Values arrive in the MongoDB readings collection and the Find and Aggregate task results land in QueryResult; the Insert task appends a new document to readings with the tracked fieldspopulate the QueryResult tag. Trace output appears in the runtime log.
The same queries can also be authored as SQL. The Dataset host translates SQL into Mongo Find or Aggregate operations. For the Find query above, a SQL form is:
| Code Block | ||
|---|---|---|
| ||
SELECT * FROM readings WHERE plant = 'Plant01' ORDER BY ts DESC LIMIT 10 |
For the supported SQL subset and what falls back to the JSON form, see the SQL subset dialect section of the connector reference page.
| Page Tree | ||||
|---|---|---|---|---|
|