实用主义 - mongodb删除重复数据
1、mongodb 查询重复数据db.tt_aa.aggregate([{$match:{ST:{$in :["xxx","yyy"]},type:0}}, # 过滤 数据{$group:{_id:{SN:'$ST',TIME:'$TT'},count:{$sum:1},dups: {$addToSet:'$_id'}}}, # 分组 计数{$match:{count:{$gt:1}}} # 匹配 count > 1的 组],{allowDiskUse:true})
2、mongodb 删除重复数据
db.tt_aa.aggregate([{$match:{ST:{$in :["xxx","yyy"]},type:0}},{$group:{_id:{SN:'$ST',TIME:'$TT'},count:{$sum:1},dups: {$addToSet:'$_id'}}},{$match:{count:{$gt:1}}}],{allowDiskUse:true}).forEach(function(doc){doc.dups.shift();db.tt_aa.remove({_id:{$in:doc.dups}});})
利用aggregate聚合查询重复数据
查询结果利用forEach进行迭代id来删除数据

mongo -logo