What would be the MongoDB C# driver equivalent of the following query using the array update operator $[]

后端 未结 1 682
耶瑟儿~
耶瑟儿~ 2021-01-26 02:37

From https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#up.S[%3Cidentifier%3E]

Given the following collection

{
   \"_id\" :          


        
相关标签:
1条回答
  • 2021-01-26 03:11

    You can try below c# using both BsonDocument and json string option. There is no linq option.

    var filter = Builders<BsonDocument>.Filter.Empty;
    var update = Builders<BsonDocument>.Update.Set("grades.$[elem].mean", 100);
    var arrayFilter = new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("elem.grade", new BsonDocument("$gte", 85)));
    var arrayFilter = new JsonArrayFilterDefinition<BsonDocument>("{ \"elem.grade\": { $gte: 85 } }");
    var arrayFilters = new List<ArrayFilterDefinition> { arrayFilter };
    var updateOptions = new UpdateOptions();
    updateOptions.ArrayFilters = arrayFilters;
    var result = collection.UpdateOne(filter, update, updateOptions);
    
    0 讨论(0)
提交回复
热议问题