Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an embedded doc (2 levels deep) in MongoDB using the official C# driver

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

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.

What is the correct syntax to update the following embedded document using the official 10 gen C# driver version 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

{ 
  "_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.

like image 937
srdemart Avatar asked Jan 23 '26 13:01

srdemart


1 Answers

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.

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#.

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.

like image 199
Robert Stam Avatar answered Jan 26 '26 02:01

Robert Stam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!