MongoDB:$ lookup数组后的$ sum

时间:2018-11-12 18:47:06

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我有这个查询,我正在使用$lookup,此后我想对查询提供的数组中的value求和。

我的查询(我正在使用猫鼬):

  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
            }
        ]
    }

我如何求和value的所有字段,并用我的第一个$ group的结果显示该总和的结果?

2 个答案:

答案 0 :(得分:3)

您可以将$sum聚合与数组值一起使用

db.user.aggregate([
  // $group stage
  { "$lookup": {
    "from": "schedule",
    "localField": "_id.phone",
    "foreignField": "customer.phone",
    "as": "user_detail"
  }},
  { "$project": {
    "total": { "$sum": "$user_detail.value" }
  }}
])

答案 1 :(得分:0)

您可以在 pipeline 聚合中使用 $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
  ])