我有一个模式,它有一个子数组,每个元素都引用另一个模式如下:
{
_id: "5fed222d806311ec81d6df23"
someOtherNestedDoc: {
_id: "abc",
someOtherField: 10
},
matches: [
{
_id: "5fed222d806311ec81d6daf3",
user: "5fed222d806311ec81d6dcf3",
probability: 10
},
{
_id: "5fed222d806311ec81d6d1f3",
user: "5fed222d806311ec81d62cf3",
probability: 20
},
]
}
我想做一个Mongoose聚合,在其中我查找每个匹配的用户引用,并且只为它项目3个字段,其中2个已经存在,第三个是该模式的数组之和,所以最后,我有这样的结果:
{
_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
},
]
}
用户看起来像这样:
{
_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
},
]
}