How to say… match when field is a number… in mongodb?

后端 未结 8 541
误落风尘
误落风尘 2021-02-04 07:41

So I have a field called \'city\' in my results...the results are corrupted, some times it\'s an actual name, sometimes it\'s a number. The following code displays all the recor

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 08:25

    db.zips.aggregate([
        {$project : {city:{$substr:["$city",0,1]},pop:1}},
        {$group:{_id:"$city",sumPop:{$sum:"$pop"}}},
        {$sort : {_id : 1}}, 
        {$match:{_id : {$regex:/\d/}} },
        {$group:{_id:"id",sumPop:{$sum:"$sumPop"}}},
    ]);
    

提交回复
热议问题