Imagine that I've the following document in collection A:
{
"_id" : 1,
"ratio" : 6.0
}
I've also the following documents in collection B:
[
{
"_id" : 1,
ratio : 3.0
},
{
"_id" : 2,
ratio : 4.0
}
]
How can I merge them together to produce the following output:
[
{
"_id" : 1,
ratio : 6.0
},
{
"_id" : 2,
ratio : 4.0
}
]
So, as you can see above:
When
collectionA._idmatches tocollectionB._id, it produces the ratio fromcollectionAas the result,When ids don't match, it takes the ratio from
collectionB.collectionAwill not have ids which doesn't exist incollectionB, because, I derivecollectionBfromcollectionAin a prior aggregation.And, for example, if there are more ids in
collectionB, they will be added to the output as well.
So, basically, collectionA overwrites collectionB and then the unmatched docs from collectionB is added to the result, as well.
This is trivial to implement in my app code however there are millions of records exist in my database so I want to do it in MongoDB. I know also that there is no full-outer-join in MongoDB, is there a way to achieve the result?