I am having a problem trying to update a property inside a list of embedded documents. The property I am trying to do a partial update on is the "SelectedDecision" in the "CaseTaskDecision" class.
public class Case
{
[BsonId]
public ObjectId InternalId { get; set; }
[BsonElement(elementName: "casetasks")]
public List<CaseTask> CaseTasks { get; set; }
}
public class CaseTask
{
[BsonId]
public ObjectId InternalId { get; set; }
[BsonElement(elementName: "caseTaskDecision")]
public CaseTaskDecision CaseTaskDecision { get; set; }
}
public class CaseTaskDecision
{
[BsonId]
public ObjectId InternalId { get; set; }
[BsonElement(elementName: "selectedDecision")]
public string SelectedDecision { get; set; }
}
Below is an example of the mongodb document
{
"_id" : ObjectId("5aff22845d02052ea80f7717"),
"casetasks" : [
{
"_id" : ObjectId("000000000000000000000000"),
"caseTaskDecision" : {
"_id" : ObjectId("000000000000000000000000"),
"selectedDecision" : null
}
}
]
}
I am using an Azure DocumentDB backend with a MongoDB interface. The C# code being used to try and update the property is:
public async Task<UpdateResult> UpdateTaskDecision(string id, string taskId, string selectedDecision)
{
var update = Builders<CaseAPI.Models.Case>.Update.Set("casetasks.$.castTaskDecision.selectedDecision", selectedDecision);
return await _db.GetCollection<CaseAPI.Models.Case>(_collection).UpdateOneAsync<CaseAPI.Models.Case>(o => o.Id.Equals(id) && o.CaseTasks.Any(t => t.Id.Equals(taskId)), update);
}
The point of concern is the "Set" statement in the above code. I am not sure how to reference the correct property to do a partial update of the embedded document.
I am very new to using MongoDB and this is the first time using the C# driver. Any pointers or links to helpful resources would be appreciated.