使用官方C#驱动程序更新MongoDB中的嵌入式文档(深层2级)

时间:2022-04-04 07:17:34

I'm having problems updating an embedded document that is 2 levels deep in a document.

我在更新文档中2级深度的嵌入式文档时遇到问题。

I've read this post Updating an embedded document in MongoDB with official C# driver, but that problem only had 1 level deep and so the syntax needs probably are different.

我已经阅读了这篇文章使用官方C#驱动程序在MongoDB中更新嵌入式文档,但该问题只有1级深度,因此语法需求可能不同。

What is the correct syntax to update the following embedded document using the official 10 gen C# driver version 1.0?

使用官方10代C#驱动程序版本1.0更新以下嵌入式文档的正确语法是什么?

{
  "_id": {
    "$oid": "4dfa2601dc1c791d40106a25"
  },
  "_t": "Model",
  "TypeId": 1,
  "Title": "Some Title",
  "ObjectBags": [
    {
      "_t": "ObjectBag",
      "_id": {
        "$oid": "4dfa2603dc1c791d40107e48"
      },
      "TypeId": 4,
      "Objects": [
        {
          "_t": "DomainObject",
          "_id": {
            "$oid": "4dfa2603dc1c791d40107e49"
          },
          "TypeId": 4,
          "ParentId": {
            "$oid": "4dfa2603dc1c791d40107e48"
          },
          "CreatedBy": "me",
          "CreatedDate": "Thu, 16 Jun 2011 08:49:21 GMT -07:00",
          "LastUpdatedBy": "me",
          "LastUpdatedDate": "Thu, 16 Jun 2011 08:49:21 GMT -07:00",
          "InactivatedDate": null,
          "Data": "1`|`11536"
        }
      ]
    }
  ]
}

This is what I've tried, I get no errors, but nothing is updated.

这是我尝试过的,我没有错误,但没有更新。

var models = _database.GetCollection<Model>("Models");
var model = models.FindOneAs<Model>(Query.EQ("_id", new ObjectId("4dfa2601dc1c791d40106a25")));

var wspwRef = model.Objects.Find(Domain.Object.Reference);
wspwRef.Set(Domain.Field.Reference.Name, "SOME REF RM");

var query = Query.EQ("ObjectBags.Objects._id", new ObjectId("4dfa2603dc1c791d40107e49"));
var documentWrapper = BsonDocumentWrapper.Create<DomainObject>(wspwRef);
models.Update(query, Update.Set("ObjectBags.Objects.$", documentWrapper));

The documentWrapper generates the following from the newly updated object

documentWrapper从新更新的对象生成以下内容

{ 
  "_id" : { "$oid" : "4dfa2603dc1c791d40107e49" }, 
  "TypeId" : 4, 
  "ParentId" : { "$oid" : "4dfa2603dc1c791d40107e48" }, 
  "CreatedBy" : "me", 
  "CreatedDate" : { "$date" : 1308239361784 }, 
  "LastUpdatedBy" : "me", 
  "LastUpdatedDate" : { "$date" : 1308239791540 }, 
  "InactivatedDate" : null, 
  "Data" : "1`|`11536^|^2`|`SOME NEW TEXT" 
}

Not sure whether the name "ObjectBags.Objects.$" is the problem or something else.

不确定名称“ObjectBags.Objects。$”是否是问题或其他问题。

1 个解决方案

#1


1  

I don't know if this is possible. Part of the problem is that you have two arrays (ObjectBags and Objects), and I've only ever seen the $ notation used with one array.

我不知道这是否可行。问题的一部分是你有两个数组(ObjectBags和Objects),我只见过一个数组使用的$符号。

In any case, with difficult update problems like this it's always best to experiment and troubleshoot in the Mongo shell, and once you get it working there you can translate the statements to C#.

在任何情况下,对于像这样的困难更新问题,最好在Mongo shell中进行实验和故障排除,一旦你在那里工作,你可以将语句翻译成C#。

You can always transfer the entire document client side and do the updates locally using C# and then Save the document back to the database. It's not atomic and if your document is very large it involves more network traffic, but it can be much simpler to do sometimes.

您始终可以传输整个文档客户端,并使用C#在本地执行更新,然后将文档保存回数据库。它不是原子的,如果你的文档非常大,它涉及更多的网络流量,但有时候做起来要简单得多。

#1


1  

I don't know if this is possible. Part of the problem is that you have two arrays (ObjectBags and Objects), and I've only ever seen the $ notation used with one array.

我不知道这是否可行。问题的一部分是你有两个数组(ObjectBags和Objects),我只见过一个数组使用的$符号。

In any case, with difficult update problems like this it's always best to experiment and troubleshoot in the Mongo shell, and once you get it working there you can translate the statements to C#.

在任何情况下,对于像这样的困难更新问题,最好在Mongo shell中进行实验和故障排除,一旦你在那里工作,你可以将语句翻译成C#。

You can always transfer the entire document client side and do the updates locally using C# and then Save the document back to the database. It's not atomic and if your document is very large it involves more network traffic, but it can be much simpler to do sometimes.

您始终可以传输整个文档客户端,并使用C#在本地执行更新,然后将文档保存回数据库。它不是原子的,如果你的文档非常大,它涉及更多的网络流量,但有时候做起来要简单得多。