使用 MongoDB 的官方 C# 驱动程序时重新保存时的预期更新行为

发布于 2024-11-15 14:25:34 字数 365 浏览 3 评论 0原文

尝试了解重新保存先前保存的现有文档(来自 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 技术交流群。

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

发布评论

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

评论(1

¢蛋碎的人ぎ生 2024-11-22 14:25:34

我已经为您的问题构建了测试:

这是我保存到 mongodb 中的文档(具有 3 级数组深度):

{
  "_id": "4dfa48d1872b4a1a50f60033",
  "NestedLevels": [
    {
      "Name": "Nested1",
      "NestedLevels": [
        {
          "Name": "Nested2",
          "NestedLevels": [
            {
              "Name": "Nested3",
              "NestedLevels": []
            }
          ]
        }
      ]
    }
  ]

}

我从 c# 加载它,用另一个值更新 Name=Nested3 并保存回来,之后我正在读取mongodb 更新了文档,它实际上也更新了。所以我猜你代码的其他部分有问题。

顺便说一句:如果您需要,我可以在这里发布 C# 中的完整测试。

更新:

测试用例:

    [TestMethod]
    public void MongodbTest()
    {
        var mongoServer = MongoServer.Create("mongodb://localhost:27020");
        var database = mongoServer.GetDatabase("StackoverflowExamples");
        var levels = database.GetCollection("levels");
        var level1 = new Level1();

        var nested1 = new LevelX() { Name = "Nested1" };
        var nested2 = new LevelX() { Name = "Nested2" };
        nested1.NestedLevels.Add(nested2);
        var nested3 = new LevelX() { Name = "Nested3" };
        nested2.NestedLevels.Add(nested3);

        level1.NestedLevels.Add(nested1);

        levels.Insert(level1);

        var item = levels.FindOneAs<Level1>();

        item.NestedLevels[0].NestedLevels[0].NestedLevels[0].Name = 
                                         "Changed nested level 3 name";

        levels.Save(item);

        var itemAfterUpdate = levels.FindOneAs<Level1>();

        Assert.AreEqual("Changed nested level 3 name", 
            itemAfterUpdate.NestedLevels[0].NestedLevels[0].NestedLevels[0].Name);

        levels.RemoveAll();
    }

文档类:

public class Level1
    {
        public Level1()
        {
            Id = ObjectId.GenerateNewId();
            NestedLevels = new List<LevelX>();
        }

        [BsonId]
        public Object Id { get; set; }

        public List<LevelX> NestedLevels { get; set; }
    }

    public class LevelX
    {
        public LevelX()
        {
            NestedLevels = new List<LevelX>();
        }

        public string Name { get; set; }

        public List<LevelX> NestedLevels { get; set; }
    }

I've builded test for your problem:

Here is document that i saved into mongodb (with 3 level array deep):

{
  "_id": "4dfa48d1872b4a1a50f60033",
  "NestedLevels": [
    {
      "Name": "Nested1",
      "NestedLevels": [
        {
          "Name": "Nested2",
          "NestedLevels": [
            {
              "Name": "Nested3",
              "NestedLevels": []
            }
          ]
        }
      ]
    }
  ]

}

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:

    [TestMethod]
    public void MongodbTest()
    {
        var mongoServer = MongoServer.Create("mongodb://localhost:27020");
        var database = mongoServer.GetDatabase("StackoverflowExamples");
        var levels = database.GetCollection("levels");
        var level1 = new Level1();

        var nested1 = new LevelX() { Name = "Nested1" };
        var nested2 = new LevelX() { Name = "Nested2" };
        nested1.NestedLevels.Add(nested2);
        var nested3 = new LevelX() { Name = "Nested3" };
        nested2.NestedLevels.Add(nested3);

        level1.NestedLevels.Add(nested1);

        levels.Insert(level1);

        var item = levels.FindOneAs<Level1>();

        item.NestedLevels[0].NestedLevels[0].NestedLevels[0].Name = 
                                         "Changed nested level 3 name";

        levels.Save(item);

        var itemAfterUpdate = levels.FindOneAs<Level1>();

        Assert.AreEqual("Changed nested level 3 name", 
            itemAfterUpdate.NestedLevels[0].NestedLevels[0].NestedLevels[0].Name);

        levels.RemoveAll();
    }

Documents classes:

public class Level1
    {
        public Level1()
        {
            Id = ObjectId.GenerateNewId();
            NestedLevels = new List<LevelX>();
        }

        [BsonId]
        public Object Id { get; set; }

        public List<LevelX> NestedLevels { get; set; }
    }

    public class LevelX
    {
        public LevelX()
        {
            NestedLevels = new List<LevelX>();
        }

        public string Name { get; set; }

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