【问题标题】:mongodb multiple $group in single querymongodb 单个查询中的多个 $group
【发布时间】:2019-01-01 19:20:26
【问题描述】:

我有类似的文档存储在 mongodb 中:

{
    "_id" : ObjectId("----"),
    "status" : "pending",
    "user" : "huSWFekrPkw_xwtqDueAm4j4tHiuPJf3",
    "type" : "inMemory",
    "question" : "Hello, How are you?",
    "intent" : "Greeting",
    "createdAt" : ISODate("2018-07-24T06:33:59.399Z"),
    "updatedAt" : ISODate("2018-07-24T06:33:59.399Z"),
}
{
    "_id" : ObjectId("----"),
    "status" : "trained",
    "user" : "huSWFekrPkw_xwtqDueAm4j4tHiuPJf3",
    "type" : "inMemory",
    "question" : "Holla",
    "intent" : "Greeting",
    "createdAt" : ISODate("2018-07-25T06:33:59.399Z"),
    "updatedAt" : ISODate("2018-07-25T06:33:59.399Z"),
}
{
    "_id" : ObjectId("----"),
    "status" : "trained",
    "user" : "huSWFekrPkw_xwtqDueAm4j4tHiuPJf3",
    "type" : "inMemory",
    "question" : "want to talk with agent?",
    "intent" : "Agent",
    "createdAt" : ISODate("2018-07-26T06:33:59.399Z"),
    "updatedAt" : ISODate("2018-07-26T06:33:59.399Z"),
}

我想要的聚合管道:

  1. intent 上的 $group
  2. status 上的 $group
  3. 根据这两组的结果,我想过滤每个特定意图的待定和训练数量。与 forGreeting intent 一样,我有 2 个待处理文档和 1 个经过培训的文档。
  4. 稍后我还想要,Greeting 意图在今天、过去 7 天或上个月有多少文档。

所以最终的文档会是这样的:

{
    "intent" : "Greeting",
    "status_pending" : 1,
    "status_trained" : 2,
    "last_day" : 1,
    "last_seven_day" : 3,
    "last_month" : 7
}
{
    "intent" : "Agent",
    "status_pending" : 1,
    "status_trained" : 1,
    "last_day" : 1,
    "last_seven_day" : 2,
    "last_month" : 3
}

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    您可以为每个intent 使用$group$push 的所有日期和状态。然后您可以使用$filter$size 计算每个过滤器有多少项:

    db.col.aggregate([
        {
            $group: {
                _id: "$intent",
                statuses: { $push: "$status" },
                dates: { $push: "$createdAt" },
            }
        },
        {
            $project: {
                _id: 0,
                intent: "$_id",
                status_pending: {
                    $size: { $filter: { input: "$statuses", as: "s", cond: { $eq: [ "$$s", "pending" ] } } }
                },
                status_trained: {
                    $size: { $filter: { input: "$statuses", as: "s", cond: { $eq: [ "$$s", "trained" ] } } }
                },
                last_day: {
                    $size: { $filter: { input: "$dates", as: "d", cond: { $gt: [ "$$d", new Date(new Date().setDate(new Date().getDate()-1)) ] } } }
                },
                last_seven_day: {
                    $size: { $filter: { input: "$dates", as: "d", cond: { $gt: [ "$$d", new Date(new Date().setDate(new Date().getDate()-7)) ] } } }
                },
                last_month: {
                    $size: { $filter: { input: "$dates", as: "d", cond: { $gt: [ "$$d", new Date(new Date().setDate(new Date().getDate()-30)) ] } } }
                },
            }
        }
    ])
    

    【讨论】:

    • 感谢您提供有用的信息和正确的查询。
    【解决方案2】:

    你也可以试试这个

    db.getCollection('collectionName').aggregate([
    {$group:{
        _id: "$intent",
        "status_pending": {$sum: {$cond: [{ $eq: ["$status", "pending"]}, 1,0 ]}},
        "status_trained": {$sum: {$cond: [{ $eq: ["$status", "trained"]}, 1,0 ]}},
        "last_day": { $sum: { $cond: [{$gt: [ "$createdAt", new Date(new Date().setDate(new Date().getDate()-1)) ]},1,0] } },
        "last_seven_day": { $sum: { $cond: [{$gt: [ "$createdAt", new Date(new Date().setDate(new Date().getDate()-7)) ]},1,0] } },
        "last_month": { $sum: { $cond: [{$gt: [ "$createdAt", new Date(new Date().setDate(new Date().getDate()-30)) ]},1,0] } }
      }
     }
    ])
    

    【讨论】:

      猜你喜欢
      • 2011-01-22
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多