0
{
  "_id": ObjectId("5882ffbe553f7c3f043fbfdf"),
  "AuditLog": null,
  "Name": "Test",
  "UserId": ObjectId("5839a1b8be46463ebc640cdc"),
  "Address": {
    "AddresType": null,
    "Address1": "S.S",
    "Address2": "Hy",
    "City": "Hyd",
    "Pincode": "50072",
    "IsActive": true,
    "Latitude": 17.497556,
    "Longitude": 78.386541
  }
}

I want to update the address object using _id

s7vr
  • 73,656
  • 11
  • 106
  • 127

1 Answers1

1

You can try something if you have the right set up. Below code finds the document with filter and builds the address embedded doc with update and executes UpdateOne with filter and update to update the address type of address doc.

var filter = Builders<User>.Filter.Eq("Id", new ObjectId("5882ffbe553f7c3f043fbfdf"));
var update = Builders<User>.Update.Set("Address.AddresType", "Home");
var result = collection.UpdateOne(filter, update);
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • That way is worked for me. Is there any way to update entire object in single query using LINQ. or using ReplaceOneAsync method – Ravi Kishore Jan 21 '17 at 20:32
  • See if this helps. http://stackoverflow.com/questions/30257013/mongodb-c-sharp-driver-2-0-update-document – s7vr Jan 21 '17 at 20:45
  • var filter = Builders.Filter.Eq("_Id", id); var result = await MongoConnectionHandler.MongoCollection.ReplaceOneAsync(filter, , new UpdateOptions { IsUpsert = true }); it will update the entire user object but i want update only address object in user – Ravi Kishore Jan 21 '17 at 20:52
  • did you try `var update = Builders.Update.Set("Address", addressobj);` ? – s7vr Jan 21 '17 at 21:02