I’m trying to get the files from the collection sensorsDataHistoric
with idSensor: 3
whose idDevice
is related to group.$id: 11
in collection devices
. An example of both kind of files:
A file from sensorsDataHistoric
:
{
"_id": {"$oid":"612c64e990977526b71d508b"},
"idSensor": {"$numberLong": "3"},
"idDevice": {"$numberLong": "1586"},
...
}
A file from devices
:
{
"_id": {"$numberLong": "1586"},
"group": {"$id": {"$numberLong": "11"}},
...
}
So I’m trying this query with an “extended” $lookup
, that doesn’t work (it never ends):
var query =
[
{
"$match":
{
"idSensor": 3
}
},
{
"$lookup":
{
"from": "devices",
"let": {"sensorsDeviceId": "$idDevice"},
"pipeline":
[
{
"$match":
{
"$expr":
{
"$and":
[
{"$eq": ["$$sensorsDeviceId", "$_id"]},
{"$eq": ["group.$id", 11]}
]
}
}
}
],
"as": "array"
}
},
{
"$unwind": "$array"
},
{
"$project":
{
...
}
}
]
db.sensorsDataHistoric.aggregate(query)
I guess the issue lies within the $and
; maybe does it get confused by mixing a variable from sensorsDataHistoric
with another one from devices
?