本文共 647 字,大约阅读时间需要 2 分钟。
案例:mongodb中有统计表(stat_list)表,存有每天的统计数据,现在需要按月计算,则需要用到mongo的聚合函数aggregate。
"_id" : ObjectId("5d00d3b66939c1efb80ba325"), "date" : "2019-06-12", "daily_count" : "164.34万",
解决方案:
1、将daily_count去掉万字,然后再转成整型相加;(PS:吐槽下,不知道为什么最原始的数据源要存成这样,最好还是不要带单位存储) 2、将date中的月份提取; 直接用substr即可。原数据:daily_count" : "164.34万" 更新后的数据:daily_count" : 164.34
db.getCollection('stat_list_copy').find().forEach( function(item){ friendsCount = item.daily_count friendsCount = (friendsCount.match(/.*?(?=万)/)) newFriendsCount = parseFloat(friendsCount[0]) db.getCollection('stat_list_copy').update({'_id':item.
转载地址:http://djffk.baihongyu.com/