代码改变世界

mongodb数据分组按字符串split

2018-12-25 17:15  slmk  阅读(2689)  评论(0编辑  收藏  举报
db.getCollection('users').aggregate([
    {$match:{ZWBH:11}},
    {$unwind:'$UUID'},
    {$project : { PM : { $split: ["$UUID.name", " "] } } },
    {$project: {PM:{ $arrayElemAt: [ "$PM", 0 ]}}},
    {$project: {PM:{ $toLower: "$PM"}}},
    {$group:{_id:'$PM',sum:{$sum:1}}},
     {$sort:{sum:-1}}
])

分组用户所用的手机品牌数量,其中UUID的格式是:

"UUID" : [ 
        {
            "id" : "xxxxxx",
            "name" : "HUAWEI DUK-AL20"
        }
    ],