3

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.

Community
  • 1
  • 1
srdemart
  • 59
  • 3
  • I think that Robert's comment is spot on here. You are trying to update an object in an array that belongs to an object in an array. The current update syntax is not very supporting of this. You either have to hard code the array location (`ObjectBags.0.Objects.0.Data`) or you have to retrieve and save the whole object. – Gates VP Jun 16 '11 at 18:02

1 Answers1

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.

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.

Robert Stam
  • 12,039
  • 2
  • 39
  • 36
  • With your alternative option of changing the entire document client side, are you suggesting pulling back the raw BsonDocument (Model), apply changes to it? – srdemart Jun 16 '11 at 18:06
  • The reason I ask is because I tried to save the deserialized model and it didn't appear to traverse the hierarchy to save nested changes. Would it if something changed at each level? – srdemart Jun 16 '11 at 18:12
  • Yes, to make the changes client side you would have to pull the entire document to the client first (either as a BsonDocument or deserialized into an instance of your domain model class). When you call Save (either on a BsonDocument or an instance of your domain model classes) the *entire* document is saved, no matter what did or didn't change. – Robert Stam Jun 17 '11 at 14:30