2017年2月28日 星期二

[ MongoDB FAQ ] How to stop insertion of Duplicate documents in a mongodb collection

Source From Here 
Question 
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 
  1. 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? 

How-To 
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
  1. db.collection.update(  
  2.    ,  
  3.    ,  
  4.   {  
  5.     upsert: <boolean>,  
  6.      multi: <boolean>,  
  7.     writeConcern:   
  8.    }  
  9.   )  
So, for your example, you could use something like this: 
  1. db.collection.update(doc, doc, {upsert:true})  
Supplement 
[ MongoDB 文件 ] Getting Started - Update Data with Java Driver

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...