MongoDB按多个子集合分组

时间:2018-07-12 11:01:27

标签: mongodb mongodb-query aggregation-framework mongodb-.net-driver

我的mongo User集合有三个子文档

   /* 1 */
{
    "_id" : 1.0,
    "FreeRegistrations" : [ 
        {
            "_id" : 1.0,
            "Code" : "1MM"
        }
    ],
    "PaidRegistrations" : [ 
        {
            "_id" : 2.0,
            "Code" : "1MM"
        }
    ],
    "CustomRegistrations" : [ 
        {
            "_id" : 3.0,
            "Code" : "1MM"
        }
    ]
}

/* 2 */
{
    "_id" : 2.0,
    "FreeRegistrations" : [ 
        {
            "_id" : 4.0,
            "Code" : "2MM"
        }
    ],
    "PaidRegistrations" : [ 
        {
            "_id" : 5.0,
            "Code" : "1MM"
        }
    ],
    "CustomRegistrations" : [ 
        {
            "_id" : 6.0,
            "Code" : "3MM"
        }
    ]
}

/* 3 */
{
    "_id" : 3.0,
    "FreeRegistrations" : [ 
        {
            "_id" : 7.0,
            "Code" : "1MM"
        }
    ],
    "PaidRegistrations" : [ 
        {
            "_id" : 8.0,
            "Code" : "2MM"
        }
    ],
    "CustomRegistrations" : [ 
        {
            "_id" : 9.0,
            "Code" : "1MM"
        }
    ]
}

我正在尝试通过代码获取所有用户注册计数。因此,从上面的集合中我想要这样的结果:

 Code      Count (Number of users)
 1MM                  3
 2MM                  2 
 3MM                  1

请提供一些在多个子集合上使用分组的方法

(Robo 3T 1.2.1)(MongoDB.Driver 2.4.4)

2 个答案:

答案 0 :(得分:0)

可以有很多方法来执行此操作,但是基本概念是您必须合并注册,按用户将其分组,然后按代码进行分组。

所以这应该对您有用:

db.user.aggregate([
   {
      $project:{
         userId:"$_id",
         registrations:{
            $setUnion:[
               "$FreeRegistrations",
               "$PaidRegistrations",
               "$CustomRegistrations"
            ]
         }
      }
   },
   {
      $unwind:"$registrations"
   },
   {
      $group:{
         _id:{
            userId:"$userId",
            code:"$registrations.Code"
         },
         userId:{
            $first:"$userId"
         },
         code:{
            $first:"$registrations.Code"
         }
      }
   },
   {
      $group:{
         _id:{
            code:"$code"
         },
         Code:{
            $first:"$code"
         },
         "Count (Number of users)":{
            $sum:1
         },

      }
   }
])

输出:

/* 1 */
{
    "_id" : {
        "code" : "2MM"
    },
    "Code" : "2MM",
    "Count (Number of users)" : 2.0
}

/* 2 */
{
    "_id" : {
        "code" : "3MM"
    },
    "Code" : "3MM",
    "Count (Number of users)" : 1.0
}

/* 3 */
{
    "_id" : {
        "code" : "1MM"
    },
    "Code" : "1MM",
    "Count (Number of users)" : 3.0
}

答案 1 :(得分:0)

db.getCollection("user").aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $project: {
                Registration: {
                    $concatArrays: ["$FreeRegistrations", "$PaidRegistrations", "$CustomRegistrations"]
                }
            }
        },

        // Stage 2
        {
            $unwind: {
                path: "$Registration",
            }
        },

        // Stage 3
        {
            $group: {
                _id: {
                    code: '$Registration.Code'
                },
                ids: {
                    $addToSet: '$_id'
                }
            }
        },

        // Stage 4
        {
            $project: {
                code: '$_id.code',
                total_users: {
                    $size: '$ids'
                },
                _id: 0
            }
        },

    ]



);