如何解决FIWARE Cygnus存储TTN层级MQTT数据为字符串的问题?
Got it, let's tackle this issue you're facing where your hierarchical TTN messages are ending up as string values instead of embedded subdocuments in MongoDB when using FIWARE's IoTAgent-JSON, Orion, and Cygnus. I've worked through similar setup quirks before, so here's a step-by-step solution:
1. Configure IoTAgent-JSON to Parse Nested Payloads
The root problem is likely that IoTAgent-JSON is treating the nested TTN payload fields as raw strings instead of structured JSON. You'll need to explicitly define these nested fields (or the entire payload object) as object type in your device provisioning template.
Option A: Map the Entire Nested Payload as an Object
If you want to preserve the full TTN payload_fields structure, define it as an object attribute in your device template:
{ "devices": [ { "device_id": "your-ttn-device-id", "entity_name": "sensor:your-device", "entity_type": "EnvironmentalSensor", "payload_type": "JSON", // Critical: tells IoTAgent to parse payload as JSON "attributes": [ { "object_id": "payload_fields", "name": "sensorReading", "type": "object" } ], "protocol": "MQTT", "transport": "MQTT" } ] }
Option B: Map Individual Nested Fields
If you only need specific nested values (e.g., temperature, humidity) as top-level or nested attributes, you can reference them directly using dot notation:
{ "devices": [ { "device_id": "your-ttn-device-id", "entity_name": "sensor:your-device", "entity_type": "EnvironmentalSensor", "payload_type": "JSON", "attributes": [ { "object_id": "payload_fields.temperature", "name": "temperature", "type": "Number" }, { "object_id": "payload_fields.humidity", "name": "humidity", "type": "Number" }, { "object_id": "payload_fields.location.coords", "name": "location", "type": "geo:json" } ], "protocol": "MQTT", "transport": "MQTT" } ] }
2. Verify Orion Context Broker Handles Nested Attributes
Orion natively supports nested structured attributes (no extra config needed for recent versions), but double-check that the entity stored in Orion has the nested structure. Run this curl command to query your entity:
curl -X GET http://<orion-host>:1026/v2/entities/sensor:your-device
You should see the nested fields as JSON objects, not strings, in the response.
3. Ensure Cygnus Stores Nested Data as MongoDB Subdocuments
Cygnus needs to be configured to convert nested JSON into MongoDB embedded documents. Check your cygnus.conf file for the MongoDB sink configuration and make sure this setting is enabled:
cygnus.sinks.mongo-sink.enable_json_nesting = true
This is usually enabled by default, but it's worth confirming—if it's set to false, Cygnus will flatten or stringify nested data.
4. Test the Flow
Send a test message from TTN, then:
- Check Orion's entity data to confirm the nested structure is present
- Query your MongoDB collection to verify the data is stored as embedded subdocuments instead of strings
That should resolve the query difficulty you're facing, since you'll be able to use MongoDB's dot notation queries (e.g., db.sensors.find({"sensorReading.temperature": {$gt: 25}})) on the nested data.
内容的提问来源于stack exchange,提问作者Juergen

