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

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

That way is worked for me. Is there any way to update entire object in single query using LINQ. or using ReplaceOneAsync method
var filter = Builders<Users>.Filter.Eq("_Id", id); var result = await MongoConnectionHandler.MongoCollection.ReplaceOneAsync(filter, <userobjec>, new UpdateOptions { IsUpsert = true }); it will update the entire user object but i want update only address object in user
did you try var update = Builders<User>.Update.Set("Address", addressobj); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.