Mongo汇总问题

1. 数据

/* 5 */
{
    "_id" : ObjectId("5902f7ca2b3fe442d60a0946"),
    "code" : "akhbxu2h1s0l",
    "pcode" : "",
    "level" : 1,
    "hasCover" : false,
    "validateNumber" : "1234",
    "validated" : false,
    "firstValidateInfo" : {
        "logId" : "",
        "clientIp" : "",
        "createAt" : ISODate("2017-04-28T08:05:30.005Z"),
        "wxNumber" : "",
        "cityName" : {
            "_id" : "",
            "name" : ""
        },
        "mobile" : ""
    },
    "productId" : "",
    "corpRecords" : [ 
        {
            "corp" : {
                "_id" : ObjectId("58fc10252b3fe46746a93fbe"),
                "name" : "udi2"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh34m6bc",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.005Z"),
                    "action" : "Handling",
                    "tag" : "京东"
                }
            ]
        }, 
        {
            "corp" : {
                "_id" : "a1",
                "name" : "北京大光2"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh34h14p",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.005Z"),
                    "action" : "Handling",
                    "tag" : "淘宝"
                }
            ]
        }, 
        {
            "corp" : {
                "_id" : "b1",
                "name" : "北京大光会才3"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh34l3qj",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.005Z"),
                    "action" : "Handling",
                    "tag" : "淘宝"
                }
            ]
        }
    ],
    "recallInfo" : {
        "_id" : "",
        "name" : ""
    },
    "createAt" : ISODate("2017-04-28T08:05:30.005Z"),
    "corpId" : "58fc10252b3fe46746a93fbe"
}

/* 6 */
{
    "_id" : ObjectId("5902f7ca2b3fe442d60a0947"),
    "code" : "b93ib987buaf",
    "pcode" : "",
    "level" : 1,
    "hasCover" : false,
    "validateNumber" : "1234",
    "validated" : false,
    "firstValidateInfo" : {
        "logId" : "",
        "clientIp" : "",
        "createAt" : ISODate("2017-04-28T08:05:30.007Z"),
        "wxNumber" : "",
        "cityName" : {
            "_id" : "",
            "name" : ""
        },
        "mobile" : ""
    },
    "productId" : "",
    "corpRecords" : [ 
        {
            "corp" : {
                "_id" : ObjectId("58fc10252b3fe46746a93fbe"),
                "name" : "udi2"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh35zoq0",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.007Z"),
                    "action" : "Handling",
                    "tag" : "当当"
                }
            ]
        }, 
        {
            "corp" : {
                "_id" : "a2",
                "name" : "天津本宏2"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh360z0p",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.007Z"),
                    "action" : "Handling",
                    "tag" : "国美"
                }
            ]
        }, 
        {
            "corp" : {
                "_id" : "b3",
                "name" : "天津本宏腾达3"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh35jtqj",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.007Z"),
                    "action" : "Handling",
                    "tag" : "京东"
                }
            ]
        }
    ],
    "recallInfo" : {
        "_id" : "",
        "name" : ""
    },
    "createAt" : ISODate("2017-04-28T08:05:30.007Z"),
    "corpId" : "58fc10252b3fe46746a93fbe"
}

/* 7 */
{
    "_id" : ObjectId("5902f7ca2b3fe442d60a0948"),
    "code" : "apy9f33v6rrw",
    "pcode" : "",
    "level" : 1,
    "hasCover" : false,
    "validateNumber" : "1234",
    "validated" : false,
    "firstValidateInfo" : {
        "logId" : "",
        "clientIp" : "",
        "createAt" : ISODate("2017-04-28T08:05:30.012Z"),
        "wxNumber" : "",
        "cityName" : {
            "_id" : "",
            "name" : ""
        },
        "mobile" : ""
    },
    "productId" : "",
    "corpRecords" : [ 
        {
            "corp" : {
                "_id" : ObjectId("58fc10252b3fe46746a93fbe"),
                "name" : "udi2"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh391n20",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.012Z"),
                    "action" : "Handling",
                    "tag" : "国美"
                }
            ]
        }, 
        {
            "corp" : {
                "_id" : "a2",
                "name" : "天津本宏2"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh38izrd",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.012Z"),
                    "action" : "Handling",
                    "tag" : "京东"
                }
            ]
        }, 
        {
            "corp" : {
                "_id" : "b4",
                "name" : "天津本宏远通3"
            },
            "userRecords" : [ 
                {
                    "_id" : "u4quh3917mj",
                    "createBy" : {
                        "_id" : "",
                        "name" : ""
                    },
                    "createAt" : ISODate("2017-04-28T08:05:30.012Z"),
                    "action" : "Handling",
                    "tag" : "国美"
                }
            ]
        }
    ],
    "recallInfo" : {
        "_id" : "",
        "name" : ""
    },
    "createAt" : ISODate("2017-04-28T08:05:30.012Z"),
    "corpId" : "58fc10252b3fe46746a93fbe"
}
View Code

 

corpRecords表示企业记录信息。每条数据中 corpRecords 数组固定索引的 corp._id 是固定的(也就是说, 企业登记顺序是固定的。) ,corpId == corpRecords.0.corp._id , 这两项保持一致。

2. 需求

查询 第每一级的条数。

如下:

 

一级企业, 二级企业,     三级企业,              三级企业总数

udi2      天津本宏2  天津本宏腾达3        60 

udi2           天津本宏2       天津本宏远通3         80

udi2           北京大光2       北京大光会才3         14

udi2           北京大光2       北京大光大略3         28

 

3.解决 

db.qRCodeInfo.aggregate(
[
 
        { $match: { "productId":  "" ,corpId:"58fc10252b3fe46746a93fbe" } }
        , {
            $group : { _id: { c1: {$slice: [ "$corpRecords.corp._id", 3 ]} }  ,
            sum:{$sum : 1 } } 
          } 
]       
)

 

posted @ 2017-04-28 16:12  NewSea  阅读(321)  评论(0编辑  收藏  举报