【问题标题】:Aggregate subdocuments, filter them by a list of ids, and group values by id聚合子文档,按 id 列表过滤它们,并按 id 分组值
【发布时间】:2019-10-30 05:29:01
【问题描述】:

我有一个需要映射的 mongo ObjectId 列表,将一些响应(可能存在也可能不存在)分配给该 id,然后根据响应生成可用性平均值。例如,我有一个引用 Users 的 ObjectId 列表:

["5d978d372f263f41cc624727", "5d978d372f263f41cc624728", "5d978d372f263f41cc624729"]

还有一个 Events 的聚合列表,其中包含一个 employeeResponses 子文档,如果他们已经回复,则记录他们的 _id、response 和任何事件 notes:

[
  {
    ...
    employeeResponses: [
      {
        _id: "5d978d372f263f41cc624727",
        response: "I want to work.",
        notes: ""
      },
      {
        _id: "5d978d372f263f41cc624728",
        response: "Available to work.",
        notes: ""
      }
    ]
  },
  {
    ...
    employeeResponses: [
      {
        _id: "5d978d372f263f41cc624727",
        response: "I want to work.",
        notes: ""
      },
      {
        _id: "5d978d372f263f41cc624728",
        response: "Prefer not to work.",
        notes: ""
      }
    ]
  },
  {
    ...
    employeeResponses: [
      {
        _id: "5d978d372f263f41cc624727",
        response: "Not available to work.",
        notes: ""
      },
      {
        _id: "5d978d372f263f41cc624729",
        response: "Not available to work.",
        notes: ""
      },
    ]
  }
]

我需要做的是在responses 数组中将他们的响应(或没有响应)分配给他们的 id:

[
  {
    "_id": "5d978d372f263f41cc624727",
    "responses": ["I want to work.", "I want to work.", "Not available to work."]
  },
  {
    "_id": "5d978d372f263f41cc624728",
    "responses": ["Available to work.", "Prefer not to work.", "No response."] 
  }
  {
    "_id": "5d978d372f263f41cc624729",
    "responses": ["No response.", "No response.", "Not available to work."] 
  }
]

然后,根据他们的响应,生成成员可用性平均值(available:["I want to work.", "Available to work"];unavailable:["Prefer not to work.", "Not available to work.", "No response."]):

[
  {
    "_id": "5d978d372f263f41cc624727",
    "availability": 66
  },
  {
    "_id": "5d978d372f263f41cc624728",
     "availability": 33,
  }
  {
    "_id": "5d978d372f263f41cc624729",
     "availability": 0,  
  }
]

在@mickl 的帮助下,我可以aggregate the responses for 1 ObjectId,但我正在努力将其扩展为 ObjectIds 列表,而不映射 ids 并为每个用户聚合 - 这似乎非常低效且成本高昂。

【问题讨论】:

    标签: mongodb mongoose aggregation-framework


    【解决方案1】:

    你可以试试这个:

    原始示例:https://mongoplayground.net/p/_pDaErcuvkP

    调整https://mongoplayground.net/p/oURNxgNiprl中的查询:

        db.collection.aggregate([
      {
        $unwind: {
          path: "$employeeResponses",          
        }
      },
      {
        "$group": {
          _id: "$employeeResponses._id",
          "availability": {
            "$sum": {
              "$cond": [
                {
                  "$in": [
                    "$employeeResponses.response",
                    [
                      "Available to work.",
                      "I want to work."
                    ]
                  ]
                },
                1,
                0
              ]
            }
          }
        }
      }
    ])
    

    【讨论】:

    • 不幸的是,这行不通,因为它不考虑缺失/不可用的响应来生成平均值:mongoplayground.net/p/_pDaErcuvkP
    • 您提出的查询与我的回答不同。无论如何,我调整了查询​​,使其适用于您在mongoplayground.net/p/_pDaErcuvkP 的示例并输入答案。指向您示例的工作查询的链接位于mongoplayground.net/p/O7CSG0Rr4Ho。
    • 很接近,但缺少没有任何响应/不可用的成员,例如5d978d372f263f41cc624729。此外,还需要一些额外的工作才能根据Events 的数量找出百分比。例如,查询返回一个总和,但它不知道总和是多少。
    • 现在我明白你的意思了。检查这个答案:mongoplayground.net/p/oURNxgNiprl
    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多