天天看點

MongoDB資料update的坑

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

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

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

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:

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.

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的操作情況:

更新已經插入的新的資料

由于唯一索引insert失敗,然後這種情況最好加一個重試的機制,這樣就可以update成功了。