升级mongo3.2.1后遇到的数据重复的问题---后续

       在api官网上,找到了关于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: trueexactly 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.

  • 与前篇提到的实验结果一致,不能满足我们应用场景。所以考虑按key分流数据

  • 对应网址:https://docs.mongodb.org/manual/reference/method/db.collection.update/#update-wc



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值