使用C#的Mongo DB UpdateOne

时间:2020-09-08 12:20:10

标签: mongodb asp.net-core

我已经在mongodb中获得了这些数据

    {"_id" : "pippo",
    "Rich" : [
        {
            "_id" : "8c9379f1-ba5a-4b43-b7ad-f5fe3bf6f09d",         
            "Appr" : null
        },
        {
            "_id" : "8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d",         
            "Appr" : null
        }
    ]},
    
    {"_id" : "pluto",
    "Rich" : [
        {
            "_id" : "8c9379f1-ba5a-4b43-b7ad-f5fe3bf6f09d",         
            "Appr" : null
        },
        {
            "_id" : "8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d",         
            "Appr" : null
        }
    ]},

如何使用命令UpdateOne在文件“ Pippo”中更新具有“ _id” 8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d的数组“ Rich”中的属性“ Appr”?

谢谢

布鲁诺

更新

我尝试

    var filter = Builders<RichiestaGiornalieri>.Filter.Eq("id", Pippo);
    var update = Builders<RichiestaGiornalieri>.Update.Set("rich.appr", new[] { new Approvazione() { Approvata = true",
    Approvatore = User.Identity.Name,
    DataApprovazione = DateTime.Now,
    id = Guid.NewGuid().ToString(),
    Note = "" } 
    });
    var arrayFilters = new List<ArrayFilterDefinition> { new JsonArrayFilterDefinition<RichiestaGiornalieri>("{'rich.id':'8c9379f1-265a-4b43-b7ad-f5fe3bf6f09d'}") };
    var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };

    dbRichieste.UpdateOne(filter, update, updateOptions);

但是我收到此错误消息

    MongoBulkWriteException`1: A bulk write operation resulted in one or more errors.
The array filter for identifier 'rich' was not used in the update { $set: { rich.appr: [ { DataApprovazione: new Date(1599579246919), _id: "2a5cc55e-70e8-4138-ace2-6b5131cd4a9a", Approvata: true, Approvatore: "Pippo", Note: "" } ] } }

0 个答案:

没有答案