Database

db={observation:[{_id:"abcd-1234",created_at:ISODate("2022-04-28T11:14:20.002+00:00"),status:"persisted",results:{stitched:{full_image:"https://www.path_to_image.com",model_outputs:["abcs-1243","abce-1247"]}}}],model_output:[{_id:"abcs-1243",created_at:ISODate("2022-04-28T11:14:20.002+00:00"),status:"persisted",results:{raw_predictions:"https://www.path_to_large_array.com",binary_mark:"https://www.path_to_binary_mask.com",predictions:["wbcs-124e","awds-234e","jnla-1233","jawd-1039"]}}],prediction:[{_id:"wbcs-124e",created_at:ISODate("2022-04-28T11:14:20.002+00:00"),status:"persisted",area:21484060.5,perimeter:1640.724417686462,bounding_box:[39,281,630,602],max_width:5751,max_height:3871,metadata:["mwao-1243","lksk-8293","psdk-1293"]}],metadata:[{_id:"mwao-1243",created_at:ISODate("2022-04-29T14:10:29.122+00:00"),status:"persisted",type:"human label",is_valid:false,comment:"this prediction is a mistake"}]}

Query

db.observation.aggregate([{$lookup:{from:"model_output",localField:"results.stitched.model_outputs",foreignField:"_id",as:"model_outputs"}},{$lookup:{from:"prediction",localField:"model_outputs.results.predictions",foreignField:"_id",as:"predictions"}},{$lookup:{from:"metadata",localField:"predictions.metadata",foreignField:"_id",as:"metadata"}},{"$addFields":{matchingDocs:{$size:{$filter:{input:"$metadata",as:"item",cond:{$and:[{$eq:["$$item.is_valid",false]},{$gte:["$$item.created_at",ISODate("2022-04-28T14:10:29.122+00:00")]},{$lte:["$$item.created_at",ISODate("2022-04-30T14:10:29.122+00:00")]}]}}}}}},{$match:{matchingDocs:{$gt:0}}},{$unset:"matchingDocs"}])

Result