MongoDB“如果当前则更新”使用 C# 的嵌入数组项
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
避免并发更新的更好方法是:
lock
从代码的一个位置更新文档的某些部分,以确保多个线程不会同时更新文档的同一部分(这肯定会成为瓶颈)。如果您需要事务——请使用关系数据库。原子更新在文档数据库中是相当新的事物,许多文档数据库只能加载和保存文档。
但是,例如,RavenDB 支持事务,但事务(特别是多个服务器之间的事务)通常会破坏可扩展性。 Mongodb 的目标 - 可扩展性,这意味着告别 ACID 和事务。
mongodb 中的最终一致性:
因此,如果您没有开始使用 mongodb 进行开发,您可以查看其他数据库。
只要确保您使用正确的工具来完成您的任务即可。
希望这有帮助。
Better way to avoid concurrent updates it is:
lock
to make sure that multiple threads not concurrently updating same part of document (it for sure will be bottleneck).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:
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.
同样的问题发布在这里:
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.