【问题标题】:$project in $lookup mongodb$lookup mongodb 中的 $project
【发布时间】:2019-05-11 14:54:03
【问题描述】:

我有一个查询,使用$lookup“加入”两个模型,之后我使用$project 选择我需要的字段,但我的 $project 带来了一系列对象 (user_detail)包含更多我需要的数据。我只想要结果的两个字段(scheduleStartscheduleEnd)。

我的查询:

 User.aggregate([{
      $match: {
        storeKey: req.body.store,      
      }
    },
    {
      $group: {
        _id: {
          id: "$_id",
          name: "$name",
          cpf: "$cpf",      
          phone: "$phone",
          email: "$email",
          birthday: "$birthday",
          lastName: "$lastname"      
        },
        totalServices: {
          $sum: "$services"
        },    
      }
    },
    {
      $lookup: {
        from: "schedules",
        localField: "_id.phone",
        foreignField: "customer.phone",
        as: "user_detail"
      }  
    },  
    {
      $project: {
        _id: 1,
        name: 1,
        name: 1,
        cpf: 1,      
        phone: 1,
        email: 1,
        birthday: 1,
        totalServices: 1,
        totalValue: { $sum : "$user_detail.value" },
        count: {
          $sum: 1
        },
        user_detail: 1
      }
    },

查询结果:

count: 1
totalServices: 0
totalValue: 73
user_detail: Array(2)
0:
...
paymentMethod: 0
paymentValue: "0"
scheduleDate: "2018-10-02"
scheduleEnd: "2018-10-02 08:40"
scheduleStart: "2018-10-02 08:20"
status: 3
store: "5b16cceb56a44e2f6cd0324b"
updated: "2018-11-27T13:30:21.116Z"
1:
...
paymentMethod: 0
paymentValue: "0"
scheduleDate: "2018-11-27"
scheduleEnd: "2018-11-27 00:13"
scheduleStart: "2018-11-27 00:03"
status: 2
store: "5b16cceb56a44e2f6cd0324b"
updated: "2018-11-27T19:33:39.498Z"
_id:
birthday: "1992-03-06"
email: "csantosgrossi@gmail.com"
id: "5bfed8bd70de7a383855f09e"
name: "Chris Santos G"
phone: "11969109995"
...

我需要的结果:

count: 1
totalServices: 0
totalValue: 73
user_detail: Array(2)
0:
scheduleEnd: "2018-10-02 08:40"
scheduleStart: "2018-10-02 08:20"
1:
scheduleEnd: "2018-11-27 00:13"
scheduleStart: "2018-11-27 00:03"

_id:
birthday: "1992-03-06"
email: "csantosgrossi@gmail.com"
id: "5bfed8bd70de7a383855f09e"
name: "Chris Santos G"
phone: "11969109995"
...

我该如何处理我的查询?

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework lookup


    【解决方案1】:

    对于 mongo 版本 > 3.6,此查询应该适合您:

     User.aggregate([{
          $match: {
            storeKey: req.body.store,      
          }
        },
        {
          $group: {
            _id: {
              id: "$_id",
              name: "$name",
              cpf: "$cpf",      
              phone: "$phone",
              email: "$email",
              birthday: "$birthday",
              lastName: "$lastname"      
            },
            totalServices: {
              $sum: "$services"
            },    
          }
        },
        {
          $lookup: {
            from: "schedules",
            localField: "_id.phone",
            foreignField: "customer.phone",
            as: "user_detail"
          }  
        },  
        {
          $project: {
            _id: 1,
            name: 1,
            name: 1,
            cpf: 1,      
            phone: 1,
            email: 1,
            birthday: 1,
            totalServices: 1,
            totalValue: { $sum : "$user_detail.value" },
            count: {
              $sum: 1
            },
            user_detail: {
                scheduleEnd: 1,
                scheduleStart: 1,
            }
          }
        },
    

    【讨论】:

    • 不得不将 all 父字段添加到投影中是一件很痛苦的事情......
    【解决方案2】:

    您可以使用$lookup 3.6 语法来$project $lookup 管道内的字段

    User.aggregate([
      { "$lookup": {
        "from": "schedules",
        "let": { "id": "$_id.phone" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": ["$customer.phone", "$$id"] }}},
          { "$project": { "scheduleStart": 1, "scheduleEnd": 1 }}
        ],
        "as": "user_detail"
      }}
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2020-08-07
      • 2019-01-29
      • 1970-01-01
      • 2019-06-12
      • 2018-08-10
      • 1970-01-01
      相关资源
      最近更新 更多