mongo aggregate 刪除重複數據

$group 按照什麼排序, 關照 _id 這個是排序的依據排序

$match 這個從排序的結果內抽取 count 大於一的內存

allDiskUse  若是內存配置比較小, 設置這個才能運行, 不然會崩。rem

而後對找出來的結果集進行刪除。it

 

 


db.all_name.aggregate([{
    $group:{_id:"$name",count:{$sum:1},dups:{$addToSet:'$_id'}} },
    { $match:{count:{$gt:1}} }],
    {allowDiskUse:true}
    ).forEach(function(it){ it.dups.shift(); db.all_name.remove({_id: {$in: it.dups}}); });io

相關文章
相關標籤/搜索