【问题标题】:Group by multiple fields and format result in mongodb?按多个字段分组并在mongodb中格式化结果?
【发布时间】:2019-02-07 20:03:33
【问题描述】:

我有一个存储数据的集合,如下所示。

{ 
    "_id" : ObjectId("5c52ba3177c895e98b3f41f7"), 
    "email" : "name@gmail.com", 
    "mobile" : "1234567890", 
    "userId" : "59b94212e4b0a459d044cd31", 
    "leadId" : "AEVUSSMS26B", 
    "leadName" : "LeadB", 
    "text" : "call client AEVUSSMS26B", 
    "reminderAt" : ISODate("2019-02-22T11:54:24.123+0000")
}
{ 
    "_id" : ObjectId("5c52bdc277c895e98b3f41f8"), 
    "email" : "name@gmail.com", 
    "mobile" : "1234567890", 
    "userId" : "59b94212e4b0a459d044cd31", 
    "leadId" : "AEVUSSMS26A", 
    "leadName" : "LeadA", 
    "text" : "call client AEVUSSMS26A", 
    "reminderAt" : ISODate("2019-02-22T11:55:24.123+0000")
}
{ 
    "_id" : ObjectId("5c52bdcd77c895e98b3f41f9"), 
    "email" : "name@gmail.com", 
    "mobile" : "1234567890", 
    "userId" : "59b94212e4b0a459d044cd31", 
    "leadId" : "AEVUSSMS26B",
    "leadName" : "LeadB",  
    "text" : "call client AEVUSSMS26B", 
    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
}
{ 
    "_id" : ObjectId("5c5403c7407b4aefc00dae39"), 
    "email" : "name@gmail.com", 
    "mobile" : "1234567890", 
    "userId" : "59b94212e4b0a459d044cd31", 
    "leadId" : "AEVUSSMS26C", 
    "leadName" : "LeadC", 
    "text" : "call client AEVUSSMS26C", 
    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
}
{ 
    "_id" : ObjectId("5c54040f407b4aefc00dae3a"), 
    "email" : "name@gmail.com", 
    "mobile" : "1234567890", 
    "userId" : "59b94212e4b0a459d044cd31", 
    "leadId" : "AEVUSSMS26C", 
    "leadName" : "LeadC", 
    "text" : "call client AEVUSSMS26C", 
    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
}
{ 
    "_id" : ObjectId("5c540426407b4aefc00dae3b"), 
    "email" : "name@gmail.com", 
    "mobile" : "1234567890", 
    "userId" : "59b94212e4b0a459d044cd31", 
    "leadId" : "AEVUSSMS26C",
    "leadName" : "LeadC",  
    "text" : "call client AEVUSSMS26C", 
    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
}
//I have 1000s such users

一个用户可以有多个潜在客户。一条线索可以有多个提醒。

我想查询和格式化结果,使得一个文档代表一个用户。每个文档将有一个潜在客户字段,用于进一步存储所有提醒。 我期待如下所示:

{ 
    "userId" : "59b94212e4b0a459d044cd31", 
    "email":"name@gmail.com",
    "mobile":"1234567890"
    "leads" : [
        {
            "_id" : {
                "leadId" : "AEVUSSMS26C", 
                "leadName" : "LeadC"
            }, 
            "leadData" : [
                {
                    "_id" : ObjectId("5c5403c7407b4aefc00dae39"), 
                    "text" : "call client AEVUSSMS26C", 
                    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
                }, 
                {
                    "_id" : ObjectId("5c54040f407b4aefc00dae3a"), 
                    "text" : "call client AEVUSSMS26C", 
                    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
                }, 
                {
                    "_id" : ObjectId("5c540426407b4aefc00dae3b"), 
                    "text" : "call client AEVUSSMS26C", 
                    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
                }
            ]
        }, 
        {
            "_id" : {
                "leadId" : "AEVUSSMS26A", 
                "leadName" : "LeadA"
            }, 
            "leadData" : [
                {
                    "_id" : ObjectId("5c52bdc277c895e98b3f41f8"), 
                    "text" : "call client AEVUSSMS26A", 
                    "reminderAt" : ISODate("2019-02-22T11:55:24.123+0000")
                }
            ]
        }, 
        {
            "_id" : {
                "leadId" : "AEVUSSMS26B", 
                "leadName" : "LeadB"
            }, 
            "leadData" : [
                {
                    "_id" : ObjectId("5c52ba3177c895e98b3f41f7"), 
                    "text" : "call client AEVUSSMS26B", 
                    "reminderAt" : ISODate("2019-02-22T11:54:24.123+0000")
                }, 
                {
                    "_id" : ObjectId("5c52bdcd77c895e98b3f41f9"), 
                    "text" : "call client AEVUSSMS26B", 
                    "reminderAt" : ISODate("2019-02-22T11:56:24.123+0000")
                }
            ]
        }
    ]
}

这是我到目前为止所做的。 我面临的问题: 1.我如何在根级别设置电子邮件,手机? 2.如何传递有限的数据作为响应而不是$$ROOT?

db.getCollection("MyCollection").aggregate([
{ 
  "$group": {
        "_id": {
            "leadId": "$leadId",
            "userId": "$userId"   
        },
        "leadData": { $push: "$$ROOT" } 
    }
},
{ 
  "$group": {
        "_id": "$_id.userId",
        "leads": { 
            "$push": "$$ROOT",
        }
    }
}

])

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    您忘记在$group 阶段使用$first 聚合

    db.collection.aggregate([
      { "$group": {
        "_id": { "leadId": "$leadId", "userId": "$userId", "leadName": "$leadName" },
        "email": { "$first": "$email" },
        "mobile": { "$first": "$mobile" },
        "leadData": {
          "$push": {
            "text": "$text",
            "_id": "$_id",
            "reminderAt": "$reminderAt"
          }
        }
      }},
      { "$group": {
        "_id": "$_id.userId",
        "email": { "$first": "$email" },
        "mobile": { "$first": "$mobile" },
        "leads": {
          "$push": {
            "_id": { "leadName": "$_id.leadName", "leadId": "$_id.leadId" },
            "leadData": "$leadData"
          }
        }
      }}
    ])
    

    【讨论】:

    • 对于leadName,我应该在管道的第一组还是第二组添加字段?
    • 谢谢伙计。但是我真的很困惑在分组时传递三个字段。我是否需要对字段进行分组才能将数据传递到管道的下一阶段?在第一个管道中是否要分组 3 次?
    • $group 聚合只知道您在 _id: <expression> 中传递的表达式,不知道其他的。因此,要访问其他字段,您必须使用 group accumulator operators
    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2015-01-08
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多