使用 MongoDB 的官方 C# 驱动程序时重新保存时的预期更新行为
尝试了解重新保存先前保存的现有文档(来自 3+ 级对象图)时的预期行为。更新是否仅应用于顶级字段,还是尝试遍历嵌套对象更改?
var models = _database.GetCollection<Model>("Models");
var model = models.FindOneAs<Model>(Query.EQ("_id", new ObjectId("4df7bce0dc1c792230912fda")));
// Apply various changes on multi-level object graph...
models.Save(model);
尝试上述操作时,嵌套更改未应用于数据库。
Trying to understand the expected behavior on re-saving a existing document (from a 3+ level object graph) that was previously saved. Is an update applied to only the top level fields or does it attempt to traverse for nested objects changes?
var models = _database.GetCollection<Model>("Models");
var model = models.FindOneAs<Model>(Query.EQ("_id", new ObjectId("4df7bce0dc1c792230912fda")));
// Apply various changes on multi-level object graph...
models.Save(model);
When trying the above, nested changes were not being applied to db.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经为您的问题构建了测试:
这是我保存到 mongodb 中的文档(具有 3 级数组深度):
}
我从 c# 加载它,用另一个值更新 Name=Nested3 并保存回来,之后我正在读取mongodb 更新了文档,它实际上也更新了。所以我猜你代码的其他部分有问题。
顺便说一句:如果您需要,我可以在这里发布 C# 中的完整测试。
更新:
测试用例:
文档类:
I've builded test for your problem:
Here is document that i saved into mongodb (with 3 level array deep):
}
I am loading it from c#, updating Name=Nested3 with another value and saving back, after that i am reading from mongodb updated document and it updated actually. So i guess problem in some other part of you code.
BTW: If you need i can post here complete test in c#.
Update:
Test case:
Documents classes: