relatedFilter section
Using a readingsFilter in the relatedFilter section of a findWithRelated query#
Insert a readingsFilter in the related section of a $findWithRelated query to return only the related items that have child telemetry items that match your query, such as HVAC air handling units with sensors that recorded matching readings.
Structure your $findWithRelated query as follows:
{ "$findWithRelated": { "parent": { "collectionDesc": { //"<property to identify a collection you want to query. In this //example, an asset collection>": "<identifying value>" }, "query": { //<simple query to find matching related items in the collection. //In this example we want to find HVAC air handling units> }, }, "relatedFilter": [ { "includeResult": true, "$or" | "$and": [ { "relatedDesc": { "<property to identify the telemetry collection you want>": "<value>" }, "readingsFilter": { "query": { //<Valid simple telemetry reading query to filter //parent related items, in this example, HVAC air //handling units> }, } "as": "sensor", } ] } ] }}
Based on the queries that match, you can return either of the following options:
- The related items with their child telemetry items
- Only the related items
In the following code example, the query fetches assets whose sensors recorded a temperature greater than 10℃:
Example request
{ "$findWithRelated": { "parent": { "query": { "properties.dtCategory.val": "HVAC Air Handling Unit" }, "collectionDesc": { "_userItemId": "asset_coll_jPQCU4n096" } }, "relatedFilter": { "includeResult": true, "$and": [ { "relatedDesc": { "_relatedUserType": "iaf_telemetry_timeseries" }, "readingsFilter": { "query": { "$and": [ { "temp": { "$gte": 10 } } ] } }, "as": "sensor" } ] } }}Sample response
{ "_list": [ { "_uri": "/nameduseritems/634cd7e89d34dd31017382e0", "_name": "Building assets Collection", "_userType": "iaf_ext_asset_coll", "_tipId": "634cd7e89d34dd31017382e1", "_versions": [ { "_userItemDbId": "634cd7e89d34dd31017382e0", "_relatedItems": { "_pageSize": 3, "_list": [ { "Asset Name": "5SPD.R1.HV.AHU.0002", "sensor": { "_pageSize": 1, "_list": [ { "_sourceId": "e3e052f9-0156-11d5-9301-0000863f27ad-00000131", "_id": "634ce4999d34dd31017382ef", "_metadata": { "_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_createdAt": 1665983641162, "_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_updatedAt": 1665983641162 } } ], "_offset": 0, "_total": 1 }, "_id": "634ce4769d34dd31017382ed", "_metadata": { "_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_createdAt": 1665983606783, "_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_updatedAt": 1665983606783 }, "properties": { "dtCategory": { "val": "HVAC Air Handling Unit" }, "Containing Floor": { "val": 1 } } }, { "Asset Name": "5SPD.R1.HV.AHU.0003", "sensor": { "_pageSize": 1, "_list": [ { "_sourceId": "a54052f9-0156-11d5-9301-0000863f27ad-000001454", "_id": "634ce4999d34dd31017382f0", "_metadata": { "_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_createdAt": 1665983641163, "_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_updatedAt": 1665983641163 } } ], "_offset": 0, "_total": 1 }, "_id": "634ce4769d34dd31017382ee", "_metadata": { "_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_createdAt": 1665983606783, "_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_updatedAt": 1665983606783 }, "properties": { "dtCategory": { "val": "HVAC Air Handling Unit" }, "Containing Floor": { "val": 2 } } } ], "_offset": 0, "_total": 3, "_filteredSize": 2 }, "_id": "634cd7e89d34dd31017382e1", "_isTip": true, "_metadata": { "_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_createdAt": 1665980392076, "_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_updatedAt": 1665980392076 }, "_version": 1 } ], "_irn": "itemsvc:nameduseritem:634cd7e89d34dd31017382e0", "_namespaces": [ "Lnt_GglJp74V" ], "_nextVersion": 2, "_shortName": "asset_coll", "_tipVersion": 1, "_versionsCount": 1, "_itemClass": "NamedUserCollection", "_userItemId": "asset_coll_jPQCU4n096", "_id": "634cd7e89d34dd31017382e0", "_metadata": { "_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_createdAt": 1665980392072, "_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec", "_updatedAt": 1665980392072 }, "_kind": "collection" } ], "_time": "82ms"}