A
A
Alexey2020-01-08 18:40:51
MongoDB
Alexey, 2020-01-08 18:40:51

Remove duplicate documents in MongoDb?

Faced such task: It is
necessary to remove duplicates in a collection provided that 'owner' == ''.

I use MongoDB and Mongoose.
How to delete these documents. The procedure will be repeated daily, records up to 20,000?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2020-01-10
@azovl

db.myCollection.find({}, {myCustomKey:1}).sort({_id:1}).forEach(function(doc){
    db.myCollection.remove({_id:{$gt:doc._id}, myCustomKey:doc.myCustomKey});
})

D
Dimonchik, 2020-01-10
@dimonchik2013

into the new one:
https://docs.mongodb.com/manual/reference/method/d...
and drop the old
one, or through an auxiliary collection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question