Mongodb split字符串后分组统计

db.getCollection(‘Information’).aggregate([{ $project : { birth : { $split: [“$birthday”, “-“] } } }, {$unwind:”$birth”}, {$match:{birth:/[0-9]{4}/}},{$group : {_id : “$birth”, num : {$sum : 1}}},{$sort:{_id:1}}])

输出结果:
/* 1 */
{
“_id” : “1996”,
“num” : 104.0
}

/* 2 */
{
“_id” : “1997”,
“num” : 118.0
}

原始数据格式:
{
“_id” : “6478413887”,
“crawl_time” : 1576841589,
“nick_name” : “12小时柠檬6478413887”,
“gender” : “男”,
“province” : “山东”,
“city” : “济南”,
“birthday” : “1979-06-13”,
“vip_level” : “未开通”,
“tweets_num” : 1,
“follows_num” : 344,
“fans_num” : 4
}

Add a Comment

您的电子邮箱地址不会被公开。