2

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.

1 Answer 1

1

Unfortunately, at the time of this writting, Azure Cosmos DB does not support updating of an individual item in an embedded array in MongoDB. We were originally using Cosmos and we have recently transitioned to MongoDB Atlas because of the lack of feature support for MongoDB.

To achieve what you want using Cosmos you would need to fetch the entire array, update the item you want (in code) and replace the entire array property of your document - quite inefficient.

It's worth checking out what Cosmos does and does not support, as there are some operations that it does not like, mainly around updating items in an embedded array, i.e PullFilter

MongoDB API support for MongoDB features and syntax

For example, taken from the article above:

Array update operators

$pull (Note: $pull with condition is not supported)

Sign up to request clarification or add additional context in comments.

1 Comment

For the time being I am using the solution to update the entire embedded document. It seems that the feature to update a property inside an embedded document is currently in development. feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/…

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.