1. <tfoot id='vVAZc'></tfoot>
    2. <legend id='vVAZc'><style id='vVAZc'><dir id='vVAZc'><q id='vVAZc'></q></dir></style></legend>
    3. <i id='vVAZc'><tr id='vVAZc'><dt id='vVAZc'><q id='vVAZc'><span id='vVAZc'><b id='vVAZc'><form id='vVAZc'><ins id='vVAZc'></ins><ul id='vVAZc'></ul><sub id='vVAZc'></sub></form><legend id='vVAZc'></legend><bdo id='vVAZc'><pre id='vVAZc'><center id='vVAZc'></center></pre></bdo></b><th id='vVAZc'></th></span></q></dt></tr></i><div id='vVAZc'><tfoot id='vVAZc'></tfoot><dl id='vVAZc'><fieldset id='vVAZc'></fieldset></dl></div>
      • <bdo id='vVAZc'></bdo><ul id='vVAZc'></ul>

    4. <small id='vVAZc'></small><noframes id='vVAZc'>

        MongoDB对同一文档的并发更新不是原子的

        时间:2023-09-28
      1. <tfoot id='TIJfN'></tfoot>

        • <bdo id='TIJfN'></bdo><ul id='TIJfN'></ul>
          1. <small id='TIJfN'></small><noframes id='TIJfN'>

            • <legend id='TIJfN'><style id='TIJfN'><dir id='TIJfN'><q id='TIJfN'></q></dir></style></legend>
                  <tbody id='TIJfN'></tbody>

                <i id='TIJfN'><tr id='TIJfN'><dt id='TIJfN'><q id='TIJfN'><span id='TIJfN'><b id='TIJfN'><form id='TIJfN'><ins id='TIJfN'></ins><ul id='TIJfN'></ul><sub id='TIJfN'></sub></form><legend id='TIJfN'></legend><bdo id='TIJfN'><pre id='TIJfN'><center id='TIJfN'></center></pre></bdo></b><th id='TIJfN'></th></span></q></dt></tr></i><div id='TIJfN'><tfoot id='TIJfN'></tfoot><dl id='TIJfN'><fieldset id='TIJfN'></fieldset></dl></div>
                • 本文介绍了MongoDB对同一文档的并发更新不是原子的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  目前,我们有一个 orderId,我们为此向用户提供好处(发布).有多个事件可以触发福利/发布.但条件是只应触发 1 个事件.因此,为了处理当前请求,我们创建了一个布尔字段 POSTING_EVENT_SENT,最初设置为 false,之后任何能够将其标记为 true 的人都可以继续进行.

                  Currently, we have an orderId for which we give the benefit(posting) to the user. There are multiple events that can trigger benefit/posting. But the condition is that only 1 event should be triggered. Hence for handling current requests, we created a boolean field POSTING_EVENT_SENT, initially set to false, and later whoever was able to mark it as true can proceed further.

                  public boolean isOrderLockedAndUpdatedToTriggerPosting(String orderId, OrderStatus orderStatus) {
                  
                         Query query = new Query();
                         query.addCriteria(Criteria.where(OrderConstants.ORDER_ID).is(orderId));
                         query.addCriteria(Criteria.where(OrderConstants.POSTING_EVENT_SENT).is(false));
                         Update update = new Update();
                         update.set(OrderConstants.ORDER_STATUS, orderStatus);
                         update.set(OrderConstants.UPDATED_AT, new Date());
                         update.set(OrderConstants.UPDATED_IP_BY, deploymentProperties.getServerIp());
                         update.set(OrderConstants.POSTING_EVENT_SENT, true);
                         update.set(OrderConstants.UPDATED_BY, OrderConstants.UPDATED_BY_WORKER);
                         UpdateResult result = mongoTemplate.updateFirst(query, update, OrderDetails.class);
                         return result.getModifiedCount() > 0;
                     }
                  

                  这是代码试图执行的 mongodb 查询

                  this is the mongodb query that code is trying to execute

                  db.order.update({orderId : 123, paymentEventSent: false},{$set : {paymentEventSent: true}})
                  

                  所以我们正在检查该字段是否为假,然后将其标记为真.所以理论上只有 1 个请求能够做到这一点,因为 mongo 查询是原子的.但是在我们的场景中,两个并发查询都成功地更新了记录.我们还缺少什么?

                  So we're checking if the field is false, then mark it to true. So theoretically only 1 request will be able to do the same, because the mongo queries are atomic. But in our scenario, both the concurrent queries were updating the records successfully. What else we're missing here?

                  推荐答案

                  使用条件更新并检查更新文档的数量以查看是否发生了更新.

                  Use conditional updates and examine the number of updated documents to see if the update happened.

                  require 'mongo'
                  
                  client = Mongo::Client.new(['localhost:14400'])
                  
                  coll = client['coll']
                  
                  coll.delete_many
                  coll.insert_one(foo: 1)
                  
                  rv = coll.update_one({foo: 1}, '$set' => {foo: 2})
                  if rv.modified_count == 1
                    puts 'Updated'
                  end
                  
                  rv = coll.update_one({foo: 1}, '$set' => {foo: 2})
                  if rv.modified_count == 1
                    puts 'Updated'
                  end
                  

                  https://github.com/p-mongo/tests/blob/master/query-conditional-update/test.rb

                  这篇关于MongoDB对同一文档的并发更新不是原子的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何返回 N1qlQueryResult 作为 Couchbase 数据库的 RE 下一篇:Jackson 使用 mixins 序列化具有动态不同名称的属性

                  相关文章

                  最新文章

                  <small id='2bgS2'></small><noframes id='2bgS2'>

                    <tfoot id='2bgS2'></tfoot>
                  1. <legend id='2bgS2'><style id='2bgS2'><dir id='2bgS2'><q id='2bgS2'></q></dir></style></legend>
                      <bdo id='2bgS2'></bdo><ul id='2bgS2'></ul>

                      <i id='2bgS2'><tr id='2bgS2'><dt id='2bgS2'><q id='2bgS2'><span id='2bgS2'><b id='2bgS2'><form id='2bgS2'><ins id='2bgS2'></ins><ul id='2bgS2'></ul><sub id='2bgS2'></sub></form><legend id='2bgS2'></legend><bdo id='2bgS2'><pre id='2bgS2'><center id='2bgS2'></center></pre></bdo></b><th id='2bgS2'></th></span></q></dt></tr></i><div id='2bgS2'><tfoot id='2bgS2'></tfoot><dl id='2bgS2'><fieldset id='2bgS2'></fieldset></dl></div>