MongoDB的C#2.0驱动程序 - 更新文件 [英] MongoDB C# Driver 2.0 - Update document

查看:188
本文介绍了MongoDB的C#2.0驱动程序 - 更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在升级我的代码MongoDB的C#2.0的驱动程序和我有升级代码更新文件的问题。

I'm currently upgrading my code to MongoDB C# driver 2.0 and I'm having issues upgrading the code to update documents.

使用老版本的,我能够做这样的事情:

using the old version I was able to do something like this:

MyType myObject; // passed in 
var collection = _database.GetCollection<MyType>("myTypes");
var result = collection.Save(myObject);



我竭力要找到一种方法在新版本中做到这一点。
我已经找到更新单个字段,如

I'm struggling to find a way to do this in the new version. I have found a few examples of updating single fields like

var filter = Builders<MyType>.Filter.Eq(s => s.Id, id);
var update = Builders<MyType>.Update.Set(s => s.Description, description);
var result = await collection.UpdateOneAsync(filter, update);



我想更新我在老版本做的方法保存的所有字段

I'd like to update all the fields as I was doing in the old version with the method Save.

任何想法?

非常感谢

推荐答案

我认为你正在寻找 ReplaceOneAsync()

MyType myObject; // passed in 
var filter = Builders<MyType>.Filter.Eq(s => s.Id, id);
var result = await collection.ReplaceOneAsync(filter, myObject)

这篇关于MongoDB的C#2.0驱动程序 - 更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆