I have two MongoDB collections with a one-to-one relationship. I want to perform an array.reduce operation based on both of these collections, like so:
CollectionA: [
{ _id: 1, valueA: 234, idB: 1 },
{ _id: 2, valueA: 64, idB: 2 },
{ _id: 3, valueA: 456, idB: 3 },
]
CollectionB: [
{ _id: 1, valueB: 678 },
{ _id: 2, valueB: 11 },
{ _id: 3, valueB: 4005 },
]
I want to calculate the sum of:
sum(valueA - valueB)
Is it possible to achieve this with a single database request? If not, what is the most elegant approach?