MongoDB數據update(upsert)的坑

統計mongodb慢查詢的時候,發現有的集合慢查詢很多,然後通知開發看一下字段加索引,

和開發討論之後加唯一索引,加的時候發現有重複數據,然後用聚合命令統計了一下24w的數據有10w+的重複數據,

開發說update操作的時候加了{upsert:true},應該是查詢不到新增一條,不會有重複數據,

然後查看mongodb的官方文檔查看db.collection.update,其中有以下解釋

Use Unique Indexes

WARNING

To avoid inserting the same document more than once, only use upsert: true if the query field is uniquely indexed.

 

Given a collection named people where no documents have a name field that holds the value Andy. Consider when multiple clients issue the following update with upsert: true at the same time:

db.people.update( { name: "Andy" }, { name: "Andy", rating: 1, score: 1 }, { upsert: true } ) 

If all update() operations complete the query portion before any client successfully inserts data, andthere is no unique index on the name field, then each update operation may result in an insert.

To prevent MongoDB from inserting the same document more than once, create a unique index on the namefield. With a unique index, if multiple applications issue the same update with upsert: true, exactly oneupdate() would successfully insert a new document.

The remaining operations would either:

  • update the newly inserted document, or

  • fail when they attempted to insert a duplicate.

    If the operation fails because of a duplicate index key error, applications may retry the operation which will succeed as an update operation.

 

 

意思大體是說:同時高併發upsert的話,查詢操作完成,但是還沒insert,這時會同時insert多條相同 的數據,爲了避免這個問題可以增加唯一索引,也就是說,數據的唯一性只能通過唯一索引來保證,

同時舉例說明了唯一索引然後高併發update的操作情況:

 

  1. 更新已經插入的新的數據
  2. 由於唯一索引insert失敗,然後這種情況最好加一個重試的機制,這樣就可以update成功了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章