Within my schema, there is a sub array where each element references another schema in the following manner:
{
_id: "5fed222d806311ec81d6df23"
someOtherNestedDoc: {
_id: "abc",
someOtherField: 10
},
matches: [
{
_id: "5fed222d806311ec81d6daf3",
user: "5fed222d806311ec81d6dcf3",
probability: 10
},
{
_id: "5fed222d806311ec81d6d1f3",
user: "5fed222d806311ec81d62cf3",
probability: 20
},
]
}
I am seeking to perform a Mongoose aggregate operation that looks up the user reference for each match and projects only 3 fields - 2 existing ones and a third one which sums an array within that schema. This would result in a structure like this:
{
_id: "5fed222d806311ec81d6df23"
someOtherNestedDoc: {
_id: "abc",
someOtherField: 10
},
matches: [
{
_id: "5fed222d806311ec81d6daf3",
user: {
firstName: "Max",
lastName: "Mustermann",
totalExpenses: 100
},
probability: 10
},
{
_id: "5fed222d806311ec81d6d1f3",
user: {
firstName: "Herbert",
lastName: "Mustermann",
totalExpenses: 200
},,
probability: 20
},
]
}
The user entities would be structured as follows:
{
_id: "5fed222d806311ec81d6dcf3",
firstName: "Max",
lastName: "Mustermann",
password: "test",
expenses: [
{
_id: 1,
price: 50
},
{
_id: 2,
price: 50
},
]
}
{
_id: "5fed222d806311ec81d62cf3",
firstName: "Herbert",
lastName: "Mustermann",
password: "test2",
expenses: [
{
_id: 3,
price: 75
},
{
_id: 4,
price: 125
},
]
}