Remove Duplicate Records MongoDB

2

I have a collection with the field called "contact_id". In my collection I have duplicate records with this key.

How can I remove duplicates, resulting in only one record? Already tried:

db.Person Duplicate.ensureIndex ({ "contact_id": 1}, {únicas: verdadeiro, dropDups: true})

But it did not work because the dropDups function is no longer available in MongoDB 3.x

I'm using 3.2

    
asked by anonymous 29.02.2016 / 20:06

2 answers

1

This example below will group the documents in the clientes collection with the nome and cidade fields. Then it will delete the duplicates. You can do that you have no error.

db.clientes.aggregate([{$group:{_id:{"nome":"$nome","cidade":"$cidade"}, dups:{$push:"$_id"},count:{$sum: 1}}},
{$match:{count: {$gt: 1}}}
]).forEach(function(doc){
    doc.dups.shift();
    db.clientes.remove({_id : {$in: doc.dups}});
});

Source: link

    
15.03.2016 / 19:16
0

I recommend you use the Upsert function at the time of inserting the data. It will look in mongodb if the chosen data already exist, if yes, the existing object will be deleted and a new one will be generated, if not, a new one will be created. The fields in the new object must be set in the function. (.Set ("Variable", data.Variable)). I leave an example below

public void UpsertDados (Dados dados)
        {
            FilterDefinition<Dados> filter = Builders<Dados>.Filter.Where(c => c.Variavel == dados.Variavel);
            UpdateDefinition<Data> updateCommand = Builders<Dados>.Update
                      .Set("Variavel", dados.Variavel);

            getCollection.UpdateOneAsync(filter, updateCommand, defaultUpdateOptions);
        }
    
18.05.2017 / 20:18