假设要查找news_id和enterprise_name组合后重复的数据,然后删除
db.getCollection('enterprise_lib_recommend').aggregate([{
'$group': {
'_id': {
'news_id': '$news_id',
'enterprise_name': '$enterprise_name'
},
'uniqueIds': {
'$addToSet': '$_id'
},
'count': {
'$sum': 1
}
}
},
{
'$match': {
'count': {
'$gt': 1
}
}
}
]).forEach(function(doc) {
doc.uniqueIds.shift();
db.getCollection('enterprise_lib_recommend').remove({
_id: {
$in: doc.uniqueIds
}
});
})