我使用C#driver在小项目中使用MongoDb,现在我坚持更新文档.

这是我的代码:

IMongoCollection<Student> studentCollection = db.GetCollection<Student>("studentV1");

Student updatedStudent = new Student() { AVG = 100, FirstName = "Shmulik" });

studentCollection.UpdateOne(
        o=>o.FirstName == student.FirstName,
            **<What should I write here?>**);

有一种简单而干净的方法来更新特定字段,比如方法ReplaceOne(updatedStudent)

推荐答案

试试这个&;for more info

IMongoCollection<Student> studentCollection = db.GetCollection<Student>("studentV1");

Student updatedStudent = new Student() { AVG = 100, FirstName = "Shmulik" });

var update = Update<Student>.
Set(s => s.AVG, "500").
Set(s => s.FirstName, "New Name");

Mongodb相关问答推荐

MongoDB:子文档中仅存在于父文档中的返回字段?

MongoDB索引使用

为什么数组长度0被认为是Mongoose中排序中最大的数字

使用Go的Mongo驱动程序,从MongoDB文档中获取元素并更新其值需要帮助

MongoDB - 来自先前匹配文档的聚合匹配字段

在 ExpirePolicy 之后从 Store 中删除元素

在 Mongo 聚合中,可以通过分组生成 3 个不同的计数

Mongoose - $project 嵌套的对象数组到数组的根级别

MongoDB乘以对象值?

Raft Vs MongoDB 初选

如何将记录从一个 mongo 数据库插入另一个?

为什么 MongoDB 配置服务器必须只有一个或三个?

无法提取地理键,经度/纬度超出范围

Select 匹配mongodb中两个字段的concat值的记录

如何使用原子操作在一个文档中切换布尔字段?

什么是 mongodb 中的admin数据库?

如何在 MongoDB Map-reduce 映射函数中使用变量

初创公司应该考虑哪些数据库系统?

MongoDB 和 Robomongo: Can't connect (authentication)

我需要手动关闭mongoose连接吗?