如何使用官方 C# 驱动程序更新 MongoDB 中嵌入文档的多个更改
我想更新文档深处各个级别的各个字段,并通过一次调用应用所有更改。理想情况下,驱动程序将使用 models.Save(model) 遍历对象模型并识别更改,但这似乎对图表中 2 层深度的更改没有影响。
所以,我现在尝试用这种方法更新整个文档,但它没有影响。知道正确的语法是什么吗?
var models = _database.GetCollection<Model>("Models");
var modelQuery = Query.EQ("_id", new ObjectId("4dfa2601dc1c791d40106a25"));
var model = models.FindOneAs<Model>(modelQuery);
// Apply various changes (including embedded docs)...
var dataRef = model.Objects.Find(Domain.Object.Reference);
dataRef.Set(Domain.Field.Reference.Name, "Some Ref Name");
models.Update(modelQuery, Update.Set("_id", BsonDocumentWrapper.Create(model)));
I want to update various fields on various levels deep within the document and apply all the changes with one call. Ideally, the driver would traverse the object model with the models.Save(model) and identify changes, but this appears to have no affect on changes made 2 levels deep in the graph.
So, I'm now trying to update the entire doc with this approach and it doesn't have an affect. Any idea on what the correct syntax would be?
var models = _database.GetCollection<Model>("Models");
var modelQuery = Query.EQ("_id", new ObjectId("4dfa2601dc1c791d40106a25"));
var model = models.FindOneAs<Model>(modelQuery);
// Apply various changes (including embedded docs)...
var dataRef = model.Objects.Find(Domain.Object.Reference);
dataRef.Set(Domain.Field.Reference.Name, "Some Ref Name");
models.Update(modelQuery, Update.Set("_id", BsonDocumentWrapper.Create(model)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前 mongodb 中可以遍历和更新的深度存在限制。
考虑这个博文示例:
即使使用 $ 运算符,您也无法更新选民数组,因为它的深度超过 2 层。
解决方案是创建一个单独的评论集合。我指的是 v 1.8
希望这会有所帮助。
There is a limitation on the level of depth you can traverse and do update in current mongodb.
Consider this blog post example:
You can't update voters array as it is deeper than 2 levels even with a $ operator.
The solution is to create a separate collection for comments. I am referring to v 1.8
Hope this helps.