Remove duplicate Mongodb records

Asked

Viewed 1,431 times

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 just one record? I’ve tried to:

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

But it didn’t work because the dropDups function is no longer available in Mongodb 3.x

I am using 3.2

2 answers

1

This example below will group the collection documents clientes across the fields nome and cidade. Then it will delete duplicates. You can do it without 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: http://codigosimples.net/2016/03/07/remover-registros-duplicados-com-mongodb/

  • This solution is perfect as it will remove duplicates always leaving the original record.

0

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

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);
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.