如何做一个UPSERT与MongoDB的2.0? [英] How to do an upsert with MongoDB 2.0?

查看:227
本文介绍了如何做一个UPSERT与MongoDB的2.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的MongoDB的接口已经完全与前一个变化。
这里你可以看到官方文档有关的一些例子如何?搜索,插入和更新而是upserts什么

The interface of MongoDB has completely changed from the previous one. Here you can see the official documentation with some examples about how to search, insert and update but what about upserts?

构想元:我试图对谷歌和SO搜索但很多资源是指旧的接口。也许这将是很好创建MongoLegacy标签。

Idea for meta: I've tried to search on google and on SO but many resources refer to the old interface. Maybe it would be nice to create a MongoLegacy tag.

推荐答案

传递的一个实例 UpdateOptions UpdateOneAsync(过滤器,更新选项),例如:

Pass an instance of UpdateOptions as the options parameter in UpdateOneAsync(filter, update, options), e.g.:

collection.UpdateOneAsync(p => p.Id == user.Id, 
    Builders<User>.Update.Set(p => p.Name, "John"), 
    new UpdateOptions { IsUpsert = true });



修改

EDIT

要替换文件,叫 ReplaceOneAsync 而不是:

To replace the document, call ReplaceOneAsync instead:

collection.ReplaceOneAsync(p => p.Id == user.Id, 
    user, 
    new UpdateOptions { IsUpsert = true });

这篇关于如何做一个UPSERT与MongoDB的2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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