我使用C#驱动程序在小项目中使用MongoDb,现在我坚持更新文档。 试图弄清楚如何更新字段 AVG (int)
这是我的代码:
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)
?
答案 0 :(得分:28)
var updateDef = Builders<Student>.Update.Set(o => o.AVG, student.AVG);
studentCollection.UpdateOne(o => o.FirstName == student.FirstName, updateDef);
我不知道要花很长时间才能找到(+2天),但我终于找到了this answer 用线:
var filter = Builders<TempAgenda>.Filter.Eq(x => x.AgendaId, agendaId);
var update = Builders<TempAgenda>.Update.Set(x => x.Items.Single(p => p.Id.Equals(itemId)).Title, title);
var result = _collection.UpdateOneAsync(filter, update).Result;
现在它更容易了。
答案 1 :(得分:5)
试试这个..&amp; 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");