Database

db={"colA":[{"_id":1,"my_id":1,"date":ISODate("2024-01-03"),"name":"id in both collection"},{"_id":3,"my_id":3,"date":ISODate("2024-01-03"),"name":"id in A but not in B"}],"colB":[{"_id":1,"status":"id in both collection"},{"_id":2,"status":"id in B but not in A"}]}

Query

db.colB.aggregate([{"$match":{_id:{$in:[1,2,3]}}},{"$lookup":{"from":"colA","localField":"_id","foreignField":"my_id","as":"orders"}},{"$set":{"orders":{"$cond":{"if":{$eq:[[],"$orders"]},"then":"$$REMOVE","else":"$orders"}}}},{"$unionWith":{"coll":"colA","pipeline":[{"$match":{_id:{$in:[1,2,3]}}},{"$lookup":{"from":"colB","localField":"my_id","foreignField":"_id","as":"orders"}},{"$match":{"orders":[]}},{"$unset":"orders"},{"$group":{"_id":"$_id","orders":{"$push":"$$ROOT"}}}]}}])

Result