通过“合并”进行 MongoDB 原子更新文档
我知道我可以通过设置特定字段来自动更新现有的 Mongo 文档。以下代码可以做到这一点:
var update = MongoDB.Driver.Builders.Update.Set("InsideLegMeasurement", 32.4);
SafeModeResult result = personCollection.Update(query, update, UpdateFlags.Multi,SafeMode.True);
但是,我可以通过传入要与现有文档“合并”的文档来自动更新多个字段吗?想象一下我有一个如下的文档: {"favcolor":"red","favfood":"pasta"} 我想用这些值更新现有文档。我想这样做:
var update = MongoDB.Driver.Builders.Update.Merge({"favcolor":"red","favfood":"pasta"});
或者甚至
var update = MongoDB.Driver.Builders.Update.Merge(myUpdateBsonDoc);
在 myBsonDocument 包含很多字段的情况下,我不想从要与原始文档合并的文档中“解压”这些字段。
这有可能吗?
谢谢
I know that I can atomically update an existing Mongo document by setting specific fields. The following code will do it:
var update = MongoDB.Driver.Builders.Update.Set("InsideLegMeasurement", 32.4);
SafeModeResult result = personCollection.Update(query, update, UpdateFlags.Multi,SafeMode.True);
However, can I atomically update several fields by passing in a document that I want to 'merge' with the existing doc? Imagine I have a document as follows:
{"favcolor":"red","favfood":"pasta"} and I want to update an existing doc with these values. I want to do this:
var update = MongoDB.Driver.Builders.Update.Merge({"favcolor":"red","favfood":"pasta"});
or even
var update = MongoDB.Driver.Builders.Update.Merge(myUpdateBsonDoc);
where myBsonDocument contains lots of fields which I don't want to have to 'unpack' from the doc that is to be merged with the original.
Is this possible somehow?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到答案:
只要你知道怎么做就很容易!
Found the answer:
Easy when you know how!