mongo 4.0
db.article.aggregate(
[{
$group: {
_id:{pmid:'$pmid'},
count:{$sum:1},
dups:{$addToSet: '$_id'},
}
},
{
$match:{count:{$gt:1}}
}],
{
allowDiskUse:true
}
).cursor.firstBatch.forEach(function(it) {
it.dups.shift();
db.article.remove({_id: {$in: it.dups}});
});
mongo 3.x
db.article.aggregate(
[{
$group: {
_id:{pmid:'$pmid'},
count:{$sum:1},
dups:{$addToSet: '$_id'},
}
},
{
$match:{count:{$gt:1}}
}],
{
allowDiskUse:true
}
).forEach(function(it) {
it.dups.shift();
db.article.remove({_id: {$in: it.dups}});
});
参考