mongodb 查重与去重

-- 查询image重复记录
db.getCollection('image').aggregate([
    { $group: { _id : {imageId: '$imageId',time:'$time'}, count: { $sum : 1 } } },
    { $match: { count: { $gt : 1} } }
])

-- 删除image重复记录
db.getCollection('image').aggregate([
    {   $group: { 
            _id : {imageId:'$imageId'},
            count: { $sum : 1 },
            dups:{$addToSet:'$_id'}
        } 
    },
    { 
        $match: {
            count: { $gt : 1} 
        }
    }
]).forEach(function(doc){
    doc.dups.shift();
    db.image.remove({_id:{$in:doc.dups}});
})

--  查询face重复记录
db.getCollection('face').aggregate([
    { $group: { _id : {faceId: '$faceId',time:'$time',personId:'$personId'}, count: { $sum : 1 } } },
    { $match: { count: { $gt : 1} } }
])

-- 删除face重复记录
db.getCollection('face').aggregate([
    {   $group: { 
             _id : {faceId: '$faceId',time:'$time',personId:'$personId'},
            count: { $sum : 1 },
            dups:{$addToSet:'$_id'}
        } 
    },
    { 
        $match: {
            count: { $gt : 1} 
        }
    }
],
{allowDiskUse: true}
).forEach(function(doc){
    doc.dups.shift();
    db.face.remove({_id:{$in:doc.dups}});
})

 

posted @ 2023-08-16 14:48  咔咔皮卡丘  阅读(292)  评论(0)    收藏  举报