Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop insertion of Duplicate documents in a mongodb collection

Let us have a MongoDB collection which has three docs..

db.collection.find()

 { _id:'...', user: 'A', title: 'Physics',   Bank: 'Bank_A' }  { _id:'...', user: 'A', title: 'Chemistry', Bank: 'Bank_B' }  { _id:'...', user: 'B', title: 'Chemistry', Bank: 'Bank_A' } 

We have a doc,

 doc = { user: 'B', title: 'Chemistry', Bank:'Bank_A' } 

If we use

 db.collection.insert(doc)  

here, this duplicate doc will get inserted in database.

 { _id:'...', user: 'A', title: 'Physics',   Bank: 'Bank_A' }  { _id:'...', user: 'A', title: 'Chemistry', Bank: 'Bank_B' }  { _id:'...', user: 'B', title: 'Chemistry', Bank: 'Bank_A' }  { _id:'...', user: 'B', title: 'Chemistry', Bank: 'Bank_A' } 

How this duplicate can be stopped. On which field should indexing be done or any other approach?

like image 257
shashank Avatar asked Jun 09 '14 14:06

shashank


People also ask

Can MongoDB store duplicate documents?

MongoDB can always store duplicate documents in the same collection regardless of the _id value. MongoDB can store duplicate documents in the same collection, as long as their _id values are different. There is no way to ensure that duplicate records are not stored in MongoDB.

How do you stop duplicates in MongoDB?

To insert records in MongoDB and avoid duplicates, use “unique:true”.


1 Answers

Don't use insert.

Use update with upsert=true. Update will look for the document that matches your query, then it will modify the fields you want and then, you can tell it upsert:True if you want to insert if no document matches your query.

db.collection.update(    <query>,    <update>,   {     upsert: <boolean>,      multi: <boolean>,     writeConcern: <document>    }   ) 

So, for your example, you could use something like this:

db.collection.update(doc, doc, {upsert:true}) 
like image 123
Vic Avatar answered Sep 30 '22 17:09

Vic