mongodb 的唯一索引,会导致update失败

MongoDB数据update的坑

统计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成功了。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值