简体   繁体   English

使用MongoDB和C#新驱动程序版本(2.0)更新嵌入式文档属性

[英]Updating an embedded document property using MongoDB and C# new driver version (2.0)

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. 我尝试对其进行部分更新的属性是“ CaseTaskDecision”类中的“ SelectedDecision”。

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 下面是mongodb文档的示例

{
    "_id" : ObjectId("5aff22845d02052ea80f7717"),
    "casetasks" : [
        {
            "_id" : ObjectId("000000000000000000000000"),
            "caseTaskDecision" : {
                "_id" : ObjectId("000000000000000000000000"),
                "selectedDecision" : null
            }
        }
    ]
}

I am using an Azure DocumentDB backend with a MongoDB interface. 我正在使用带有MongoDB界面的Azure DocumentDB后端。 The C# code being used to try and update the property is: 用于尝试和更新属性的C#代码为:

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. 关注点是上述代码中的“ Set”语句。 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. 我对使用MongoDB非常陌生,这是第一次使用C#驱动程序。 Any pointers or links to helpful resources would be appreciated. 任何指向有用资源的指针或链接将不胜感激。

Unfortunately, at the time of this writting, Azure Cosmos DB does not support updating of an individual item in an embedded array in MongoDB. 不幸的是,在撰写本文时,Azure Cosmos DB 支持在MongoDB中的嵌入式阵列中更新单个项目。 We were originally using Cosmos and we have recently transitioned to MongoDB Atlas because of the lack of feature support for MongoDB. 我们最初使用Cosmos,但由于缺乏对MongoDB的功能支持,我们最近已过渡到MongoDB Atlas。

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. 要使用Cosmos实现所需的功能,您需要获取整个数组,更新所需的项(在代码中)并替换文档的整个array属性-效率很低。

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, ie PullFilter 值得检查一下Cosmos的功能和不支持的功能,因为它不喜欢某些操作,主要是围绕更新嵌入式数组中的项,即PullFilter

MongoDB API support for MongoDB features and syntax MongoDB API对MongoDB功能和语法的支持

For example, taken from the article above: 例如,摘自以上文章:

Array update operators 数组更新运算符

$pull (Note: $pull with condition is not supported) $ pull(注意:不支持带条件的$ pull)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM