readingsfilter-in-findwithrelated-parent
Using a readingsFilter in the parent section of a findWithRelated query
Insert a readingsFilter in the parent section of a $findWithRelated query to return telemetry items whose readings match your query and then a related collection query, such as their relationship to assets on a certain floor number.
Structure your $findWithRelated query as follows:
{
"$findWithRelated": {
"parent": {
"collectionDesc": {
"<property to identify telemetry collections>": "<value>"
},
"query": {
//<simple query to find parent related items in the collections
//you describe in parent.collectionDesc>
},
"readingsFilter": {
"query": {
//<Valid simple query to filter parent telemetry item query for
//telemetry items that match this telemetry reading query>
},
}
},
"related": [
{
"relatedDesc": {
"<property to identify related collections by, in this case asset collections>": "<value>"
},
"as": "asset",
"query": {
//<simple query to find related items in the collections you
//describe in related.relatedDesc. In this example, we want to
//query the first floor property>
}
}
]
}
}
The following code example returns a list of sensors that recorded a temperature above 11℃ on or after 09:00, December 1st, 2021, and are on the first floor:
Example request
{
"$findWithRelated": {
"parent": {
"collectionDesc": {
"_userItemId": "tempsensor_2ZoU1RwTKm"
},
"options": {
"project": {
"_sourceId": 1
}
},
"query": {
"dtType": "Temprature Sensor"
},
"readingsFilter": {
"query": {
"$and": [
{
"temp": {
"$gte": 11
}
},
{
"_ts": {
"$gte": "2021-12-01T09:00:00.000Z"
}
}
]
}
}
},
"related": [
{
"relatedDesc": {
"_relatedUserType": "iaf_ext_asset_coll",
"_isInverse": true
},
"as": "asset",
"query": {
"properties.Containing Floor.val": 1
}
}
]
}
}
Example response
{
"_list": [
{
"_uri": "/nameduseritems/634cc91c9d34dd31017382d3",
"_name": "Temperature Sensors Collection",
"_userType": "iaf_telemetry_timeseries",
"_tipId": "634cc91c9d34dd31017382d4",
"_versions": [
{
"_userItemDbId": "634cc91c9d34dd31017382d3",
"_relatedItems": {
"_pageSize": 1,
"_list": [
{
"_sourceId": "a2e052a6-9126-21d5-7301-0000863f27ad-3450121",
"_id": "634ccbe19d34dd31017382dc",
"asset": {
"_pageSize": 1,
"_list": [
{
"Asset Name": "AHU-1",
"_id": "634cd93a9d34dd31017382e2",
"_metadata": {
"_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec",
"_createdAt": 1665980730755,
"_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec",
"_updatedAt": 1665980730755
},
"properties": {
"Room Number": {
"val": 1
}
},
"Containing Floor": {
"val": 1
}
}
],
"_offset": 0,
"_total": 1
}
}
],
"_offset": 0,
"_total": 1
},
"_id": "634cc91c9d34dd31017382d4",
"_isTip": true,
"_metadata": {
"_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec",
"_createdAt": 1665976605041,
"_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec",
"_updatedAt": 1665976605041
},
"_version": 1
}
],
"_irn": "itemsvc:nameduseritem:634cc91c9d34dd31017382d3",
"_namespaces": [
"Lnt_GglJp74V"
],
"_nextVersion": 2,
"_shortName": "tempsensor",
"_tipVersion": 1,
"_versionsCount": 1,
"_itemClass": "NamedTelemetryCollection",
"_userItemId": "tempsensor_2ZoU1RwTKm",
"_id": "634cc91c9d34dd31017382d3",
"_description": "Temperature Sensor IoT",
"_metadata": {
"_updatedById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec",
"_createdAt": 1665976604777,
"_createdById": "057ce6fd-a306-4bdc-8514-0a45c453f7ec",
"_updatedAt": 1665976604777
},
"_kind": "collection"
}
],
"_time": "135ms"
}