title: Updating an embedded doc (2 levels deep) in MongoDB using the official C# driver
body: <p>I'm having problems updating an embedded document that is 2 levels deep in a document.  </p>
  
  <p>I've read this post <a href="https://stackoverflow.com/questions/4378719/updating-an-embedded-document-in-mongodb-with-official-c-driver/4379449#4379449">Updating an embedded document in MongoDB with official C# driver</a>, but that problem only had 1 level deep and so the syntax needs probably are different.</p>
  
  <p>What is the correct syntax to update the following embedded document using the <a href="http://www.mongodb.org/display/DOCS/CSharp+Language+Center" rel="nofollow noreferrer">official 10 gen C# driver</a> version 1.0?</p>
  
  <pre><code>{
    "_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"
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>This is what I've tried, I get no errors, but nothing is updated.</p>
  
  <pre><code>var models = _database.GetCollection&lt;Model&gt;("Models");
  var model = models.FindOneAs&lt;Model&gt;(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&lt;DomainObject&gt;(wspwRef);
  models.Update(query, Update.Set("ObjectBags.Objects.$", documentWrapper));
  </code></pre>
  
  <p>The documentWrapper generates the following from the newly updated object</p>
  
  <pre><code>{ 
    "_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" 
  }
  </code></pre>
  
  <p>Not sure whether the name "ObjectBags.Objects.$" is the problem or something else.</p>