Node.js MongoDB:$查找数组后求和
我有这个查询,我正在使用,在此之后,我想对lookup提供的数组中的Node.js MongoDB:$查找数组后求和,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有这个查询,我正在使用,在此之后,我想对lookup提供的数组中的值进行求和 我的查询(我正在使用猫鼬): User.aggregate([{ $match: { storeKey: req.body.store, } }, { $group: { _id: { id: "$_id", name: "$name", cpf: "$cpf",
值进行求和
我的查询(我正在使用猫鼬):
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{ $project: { "user_detail.value": 1 } },
])
{
"_id": {
"id": "5bd89899bda5c343749d00f0",
"name": "marcio",
"cpf": null,
"phone": "11999999999",
"email": "marcio@gmail.com",
"birthday": "2018-10-30T17:44:57.834Z"
},
"user_detail": [
{
"value": 50
},
{
"value": 50
},
{
"value": 40
},
{
"value": 20
},
{
"value": 20
},
{
"value": 50
},
{
"value": 18
},
{
"value": 20
},
{
"value": 20
},
{
"value": 10
},
{
"value": 120
},
{
"value": 50
},
{
"value": 120
},
{
"value": 20
},
{
"value": 20
},
{
"value": 25
},
{
"value": 20
},
{
"value": 20
},
{
"value": 20
},
{
"value": 10
},
{
"value": 20
},
{
"value": 20
},
{
"value": 35
},
{
"value": 20
},
{
"value": 20
}
]
}
我的查询结果(样本收集):
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{ $project: { "user_detail.value": 1 } },
])
{
"_id": {
"id": "5bd89899bda5c343749d00f0",
"name": "marcio",
"cpf": null,
"phone": "11999999999",
"email": "marcio@gmail.com",
"birthday": "2018-10-30T17:44:57.834Z"
},
"user_detail": [
{
"value": 50
},
{
"value": 50
},
{
"value": 40
},
{
"value": 20
},
{
"value": 20
},
{
"value": 50
},
{
"value": 18
},
{
"value": 20
},
{
"value": 20
},
{
"value": 10
},
{
"value": 120
},
{
"value": 50
},
{
"value": 120
},
{
"value": 20
},
{
"value": 20
},
{
"value": 25
},
{
"value": 20
},
{
"value": 20
},
{
"value": 20
},
{
"value": 10
},
{
"value": 20
},
{
"value": 20
},
{
"value": 35
},
{
"value": 20
},
{
"value": 20
}
]
}
如何对值的所有字段求和,并将此和的结果与我的第一个$group的结果一起显示?您可以使用数组值的聚合
db.user.aggregate([
// $group stage
{ "$lookup": {
"from": "schedule",
"localField": "_id.phone",
"foreignField": "customer.phone",
"as": "user_detail"
}},
{ "$project": {
"total": { "$sum": "$user_detail.value" }
}}
])
您可以在$lookup
聚合中使用管道
db.user.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$lookup: {
from: "schedules",
as: "user_detail",
let: {user_detail: "$phone"}
pipeline: [
{
// you could use a match on the second collection for example:
country: 'FR'
},
{
$group:{
_id: null,
count: {
$sum: "$value"
}
}
},
// More stages to add
]
}
},
// More stages to add
])