Removing duplicate records using MapReduce

后端 未结 4 1299
借酒劲吻你
借酒劲吻你 2020-12-10 06:09

I\'m using MongoDB and need to remove duplicate records. I have a listing collection that looks like so: (simplified)

[
  { \"MlsId\": \"12345\"\" },
  { \"M         


        
4条回答
  •  猫巷女王i
    2020-12-10 06:49

    this is how I following the @harri answer to remove duplicates:

    //contains duplicated documents id and numeber of duplicates 
    db.createCollection("myDupesCollection")
    res = db.sampledDB.mapReduce(m, r, { out : "myDupesCollection" });
    
    // iterate through duplicated docs and remove duplicates (keep one) 
    db.myDupesCollection.find({value: {$gt: 1}}).forEach(function(myDoc){
        u_id = myDoc._id.MlsId;
        counts =myDoc.value;
        db.sampledDB.remove({MlsId: u_id},counts-1); //if there are 3 docs, remove 3-1=2 of them
    });
    

提交回复
热议问题