How to use $ positional operator in MongoDB C# driver version 2

后端 未结 1 641
野的像风
野的像风 2020-12-17 00:17

I need to update a field of one element from array sub document of a document.

MongoDB have the $ positional operator to do this. But in MongoDB C# driver version 2

1条回答
  •  醉梦人生
    2020-12-17 00:53

    You can try something like this.

    var builder = Builders.Filter;
    var filter = builder.Eq(student=> student.Id, 1) & builder.ElemMatch(student => student.Grades, x => x == 80);
    
    var builder = Builders.Update;
    var update = builder.Set(student => student.Grades[-1], 82);
    
    var result = collection.UpdateOne(filter, update);
    

    0 讨论(0)
提交回复
热议问题