I have a schema that has a sub array and each of its elements reference another schema like so:
{
_id: "5fed222d806311ec81d6df23"
someOtherNestedDoc: {
_id: "abc",
someOtherField: 10
},
matches: [
{
_id: "5fed222d806311ec81d6daf3",
user: "5fed222d806311ec81d6dcf3",
probability: 10
},
{
_id: "5fed222d806311ec81d6d1f3",
user: "5fed222d806311ec81d62cf3",
probability: 20
},
]
}
I want to do a Mongoose aggregate, in which I lookup the user reference for every match, and only project 3 fields for it, 2 are already existing, the third one is the sum of an array of that schema, so that in the end, I have something 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
},
]
}
And the users would look like this:
{
_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
},
]
}