【问题标题】:Concurrency issues using bulkWrite on a MongoDb collection在 MongoDb 集合上使用 bulkWrite 的并发问题
【发布时间】:2023-02-17 06:32:30
【问题描述】:

我使用多个进程,它们并行运行以更新 MongoDb 集合中的日期。我使用这样的东西:

db.collection.bulkWrite(
   [
      { updateOne : <document> },
      { updateOne : <document> },
      { updateOne : <document> },
      { updateOne : <document> },
   ],
   { ordered : false }
)

updateOne 查询经常更新同一个文档,但在文档不同的树中。我的查询看起来像这样:

{
    '_id': 'ABC12345',
    'idents.tree.subtreeId': $anyIdHere,
}

和更新集:

{ '$set': {
    'idents.$.tree.entry': $newDictHere,
} }

如果两个进程现在对同一个文档执行bulkWrite(即ABC12345),但对于不同的subtreeIds,则第一个bulkWrite及其updateOnes不会保留在文档中。看起来存在并发问题,但我不知道是什么原因造成的。是否有任何我可能遗漏的 MongoDb 限制?我使用 pymongo 任何 Python 来执行 bulkWrite

【问题讨论】:

  • 您能否提供文档和更新的最小工作示例?

标签: mongodb concurrency pymongo


【解决方案1】:

当 2 个线程尝试同时更新同一个文档时,您的并发问题似乎与文档操作的原子性有关。

Link to MongoDB docs

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多