Database
db={users:[{id:1,name:"Aravinth",prices:[{beginDate:"2022-08-24T07:29:01.639Z",endDate:"2022-08-31T07:29:01.639Z",price:45}]},{id:2,name:"Raja",prices:[{beginDate:"2022-07-25T07:29:01.639Z",endDate:"2022-07-30T07:29:01.639Z",price:55}]}],datasets:[{color:"braun, rose gold",firstBillable:"2022-08-24T07:29:01.639Z",assignedTo:1},{color:"beige, silber",firstBillable:"2022-07-25T07:29:01.639Z",assignedTo:2}]}
Query
db.datasets.aggregate([{"$lookup":{"from":"users","as":"details",let:{assigned_to:"$assignedTo",first_billable:"$firstBillable"},pipeline:[{"$match":{$expr:{"$and":[{"$eq":["$id","$$assigned_to"]},{"$gte":["$first_billable","$details.prices.beginDate"]},{"$lte":["$first_billable","$details.prices.endDate"]}]}}}]}},{"$addFields":{"details":0,"cg":{$first:{"$first":"$details.prices.price"}}}}])