MongoDB“如果当前则更新”使用 C# 的嵌入数组项

发布于 2024-11-20 00:36:33 字数 854 浏览 3 评论 0原文

在 MongoDB 中,使用 C# 驱动程序,仅当嵌入数组的元素自第一次检索以来未曾更改时,如何才能替换该元素。本质上,这将是嵌入数组元素的“如果当前则更新”。

例如,假设在步骤 #1 中我检索以下文档:

{
  "_id": {
    "$oid": "4defe15e2a66bc11986859bb"
  },
  "widgets": [
    {
      "_id": "12312312",
      "views": 3,
      "comments": 7
    },
    {
      "_id": "567567FF",
      "views": 0,
      "comments": 1
    },
    {
      "_id": "890TT890",
      "views": 2,
      "comments": 8
    }
  ],
  "dtcreate": "Wed, 08 Jun 2011 16:53:51 GMT -04:00",
}

然后在步骤 #2 中我提取小部件“12312312”的对象并对其进行一些更改,以便更新的小部件为:

    {
      "_id": "12312312",
      "views": 5,
      "comments": 9
    }

现在在步骤 #3 中我使用位置运算符仅更新文档中的此特定小部件。

这里的一切都运行良好,但唯一的问题是我不知道在步骤 #1 和步骤 #3 之间小部件“12312312”是否发生了另一个更新。

我正在寻找的是一种干净的方法来取消步骤#3中的更新,如果在#1和#3之间对小部件(或者甚至整个文档,如果不能在小部件级别完成)进行了任何更新。

In MongoDB, using the C# driver, how can one replace an element of an embedded array ONLY if it hasn't been changed since it was first retrieved. In essence this would be an "Update if Current" of an embedded array element.

For example let's say in STEP #1 I retrieve the following document:

{
  "_id": {
    "$oid": "4defe15e2a66bc11986859bb"
  },
  "widgets": [
    {
      "_id": "12312312",
      "views": 3,
      "comments": 7
    },
    {
      "_id": "567567FF",
      "views": 0,
      "comments": 1
    },
    {
      "_id": "890TT890",
      "views": 2,
      "comments": 8
    }
  ],
  "dtcreate": "Wed, 08 Jun 2011 16:53:51 GMT -04:00",
}

Then in STEP #2 I extract the object of widget "12312312" and make some changes to it so the updated widget is:

    {
      "_id": "12312312",
      "views": 5,
      "comments": 9
    }

Now in STEP #3 I use the positional operator to update only this specific widget in the document.

Everything here works well, but the only problem is I wouldn't know if another update happened to widget "12312312" between STEP #1 and STEP #3.

What I'm seeking is a clean way to cancel the update in STEP #3 if any update took place to the widget (or even the entire document if it can't be done at the widget level) between #1 and #3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云仙小弟 2024-11-27 00:36:33

避免并发更新的更好方法是:

  1. 更新小部分(如果不需要,则不是整个小部件)。
  2. 使用lock从代码的一个位置更新文档的某些部分,以确保多个线程不会同时更新文档的同一部分(这肯定会成为瓶颈)。
  3. 设计数据库以避免同时更新文档的相同部分。

如果您需要事务——请使用关系数据库。原子更新在文档数据库中是相当新的事物,许多文档数据库只能加载和保存文档。

但是,例如,RavenDB 支持事务,但事务(特别是多个服务器之间的事务)通常会破坏可扩展性。 Mongodb 的目标 - 可扩展性,这意味着告别 ACID 和事务。

mongodb 中的最终一致性

MongoDB 依赖于最终一致性,而不是严格一致性。那
意味着,当发生写入并且写入命令返回时,我们不能
100% 确定从那一刻起,所有其他流程
只会看到更新的数据。他们最终只能
看到变化。这会影响缓存,因为我们无法使之无效
立即重新填充我们的缓存,但同样,在 Web 应用程序中
如果更新需要几秒钟才能传播,通常没什么大不了的

因此,如果您没有开始使用 mongodb 进行开发,您可以查看其他数据库。

只要确保您使用正确的工具来完成您的任务即可。

希望这有帮助。

Better way to avoid concurrent updates it is:

  1. Update small parts (not entire widget if it not required).
  2. Update some part of document from one place of code with lock to make sure that multiple threads not concurrently updating same part of document (it for sure will be bottleneck).
  3. Design your database so to avoid concurrent updates of same pieces of a document.

If you need transactions -- use relational databases. Atomic updates is pretty new thing in document databases, many document databases just can load and save documents back.

But, for example, RavenDB support transactions, but transactions (in particular between multiple servers) usually killing scalability. Mongodb goal - scalability and that's mean bye bye ACID and transactions.

Eventual consistency in mongodb:

MongoDB relies on eventual consistency, not strict consistency. That
means, when a write occurs and the write command returns, we can not
be 100% sure that from that moment in time on, all other processes
will see the updated data only. They will only eventually be able to
see the changes. This affects caching, because we can’t invalidate and
re-fill our caches immediately, but again, in web applications it’s
often not a big deal if updates take a few seconds to propagate

So if you aren't started development with mongodb you can review other databases.

Just make sure that you using right tool for your tasks.

Hope this helps.

初心未许 2024-11-27 00:36:33

同样的问题发布在这里:

http://groups.google.com /group/mongodb-user/browse_thread/thread/888f5e14145d9f0c#

当讨论集中在一起时通常效果最好 地方。

This same question was posted here:

http://groups.google.com/group/mongodb-user/browse_thread/thread/888f5e14145d9f0c#

It usually works best when the discussion is consolidated in one place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文