Filtering one collection using DBRef from a second one that refers to another third

Hi, @Prasad_Saya, thanks a lot for your answer.

I came out with something more obvious:

var pipeline = 
[
	{
		"$match": {"group.$id": 11}
	},
	{
        "$lookup":
        {
            "from": "sensorsData",
            "localField": "_id",
            "foreignField": "idDevice",
            "as": "array"
        }
    },
    {
        "$unwind": "$array"
    },
    {
		"$match": {"array.idSensor": 3}
	}, 
...
]

db.devices.aggregate(pipeline)

After the last $match there are a couple of $addFields and a $project.

Your solution looks far better; mine doesn’t filter by idSensor until doing the $unwind, so I’m carrying tons of files I don’t need during first stages. Im trying to implement the query in a BI application and the server is unable to load the data, I always end up getting a timeout error.

I’ll try to fix mine using yours, thanks again!