Database

db={"testcoll":[{"location_id":1,"probes":[{"probe_id":2,"readings":[{"value":42,"when":5},{"value":37,"when":10},{"value":43,"when":15},{"value":41,"when":20}]}]}],"testcoll1":[{}]}

Query

db.testcoll.aggregate([{"$match":{"location_id":{"$eq":1}}},{"$set":{"prob-id":{"$cond":[{"$eq":["$probes",[]]},4,2]}}},{"$set":{"probes":{"$cond":[{"$eq":["$probes",[]]},[{"probe_id":"$prob-id","readings":[]}],"$probes"]}}},{"$lookup":{"from":"testcoll1","let":{"probes":"$probes"},"pipeline":[{"$set":{"probes":{"$concatArrays":["$$probes",[{"probe_id":2,"readings":[{"value":43,"when":11}]}]]}}},{"$unwind":{"path":"$probes"}},{"$unwind":{"path":"$probes.readings"}},{"$sort":{"probes.probe_id":1,"probes.readings.when":1}},{"$replaceRoot":{"newRoot":"$probes"}}],"as":"probes"}},{"$set":{"probes":{"$reduce":{"input":"$probes","initialValue":[],"in":{"$let":{"vars":{"ps":"$$value","p":"$$this"},"in":{"$let":{"vars":{"prv_p":{"$last":"$$ps"}},"in":{"$cond":[{"$and":["$$prv_p",{"$eq":["$$prv_p.readings.value","$$p.readings.value"]},{"$eq":["$$prv_p.probe_id","$$p.probe_id"]},{"$eq":["$$p.probe_id",2]}]},"$$ps",{"$concatArrays":["$$ps",["$$p"]]}]}}}}}}}}},{"$lookup":{"from":"testcoll1","let":{"probes":"$probes"},"pipeline":[{"$set":{"probes":"$$probes"}},{"$unwind":{"path":"$probes"}},{"$replaceRoot":{"newRoot":"$probes"}},{"$group":{"_id":"$probe_id","readings":{"$push":"$readings"}}},{"$set":{"probe_id":"$_id"}},{"$project":{"_id":0}}],"as":"probes"}},{"$unset":["_id","prob-id"]}])

Result