I'm trying to properly write a query that will join objects from two collections. In one collection (A) I have objects with fields and array of users for example, each one of the user objects has the userDetailsId that I'm going to use when joining/merging each one of the users with each one of his userDetails from Collection B.
Collection A:
{
"_id": "1",
"field1": "value1",
"field2": "value2",
"users": [
{
"userField1": "value3",
"userField2": "value4",
"userDetailsId": "100"
},{
"userField1": "value5",
"userField2": "value6",
"userDetailsId": "200"
}
]
}
Collection B:
{
"_id": "100",
"field1": "value1",
"field2": "value2",
"object1": {
"field3": "value3",
"field4": "value4"
},
"object2": {
"field5": "value5",
"field6": "value6"
}
},
{
"_id": "200",
"field1": "value1",
"field2": "value2",
"object1": {
"field3": "value3",
"field4": "value4"
},
"object2": {
"field5": "value5",
"field6": "value6"
}
}
Preferred result:
{
"_id": "1",
"field1": "value1",
"field2": "value2",
"users": [
{
"userField1": "value3",
"userField2": "value4",
"userDetailsId": "100",
"userDetails":{
"_id": "100",
"field1": "value1",
"field2": "value2",
"object1": {
"field3": "value3",
"field4": "value4"
},
"object2": {
"field5": "value5",
"field6": "value6"
}
}
},{
"userField1": "value5",
"userField2": "value6",
"userDetailsId": "200",
"userDetails":{
"_id": "200",
"field1": "value1",
"field2": "value2",
"object1": {
"field3": "value3",
"field4": "value4"
},
"object2": {
"field5": "value5",
"field6": "value6"
}
}
}
]
}
One of the queries i have written but without getting preferred result:
db.collectiona.aggregate([{$match: {'_id':1}},{$lookup:{from:"collectionb", localField: "users.userdetailsid", foreignField: "_id", as:"userdetails"}}
If I run this I will just merge these two collections and will have all the matched userdetails objects from collection B inside the result as an array of userdetails.
Since my goal here is to merge users and their userdetails in user objects I then tried to put as: "users.userdetails" but that will replace user with userdetails data (haven't found a possible way to put upsert to true and add whole userdetails object in user when userdetailsid is matched).
After that I then tried to pipe the result of aggregation and tried to put userdetails inside user object with query
db.collectiona.aggregate([{$match: {'_id':1}},{$lookup:{from:"collectionb", localField: "users.userdetailsid", foreignField: "_id", as:"userdetails"},
{"$addFields":{
"user.userdetails":{
"$map":{
"input": "$shifts",
"in":{
"$mergeObjects":["$$this",{"$arrayElemAt":["$collectionb",{"$indexOfArray":["$collectionb.id","$$this.userdetailsid"]}]}]
}
}
}
}}
}
But this was also not good.
Do you have any ideas how could I properly write query to get this kind of a result?