将我的数组对象的新字段添加到现有文档mongodb C#驱动程序最新2.7

时间:2019-01-09 09:11:47

标签: c# mongodb

在MongoDB中,我有一个如下文件

"name":"test2",
    "age":30,
    "data":[        
        {
            "data11":"val11",
            "data12": "val12",
            "data13": "val13"
        },
        {
            "data21" : "val21",
            "data22":"val22",
            "data23": "val23"
        }
        ],
    }).

通过C#,我想像这样向第二个数据数组添加一个新字段。

"name":"test2",
"age":30,
"data":[        
    {
        "data11":"val11",
        "data12": "val12",
        "data13": "val13",
        "data14": "val14"
    },
    {
        "data21" : "val21",
        "data22":"val22",
        "data23": "val23",
        "data24": "val24"

    }
    ],
}).

我在c#驱动程序2.7中写了这样的最新代码。

MongoClient client = new MongoClient("mongodb://localhost:27017");
        IMongoDatabase database = client.GetDatabase("test");
        var collection = database.GetCollection<BsonDocument>("entities");
var filter = Builders<BsonDocument>.Filter.Eq("data.data11", "val11");
        var update = Builders<BsonDocument>.Update.Set("data.$.data14", "val14");
        var res = collection.UpdateOneAsync(filter, update);

在C#代码上方,我可以添加第一个数据数组。但是无法添加第二个数组新字段。有人可以建议我吗?

0 个答案:

没有答案