如何使用 MongoDB 2.0 进行更新插入?
How to do an upsert with MongoDB 2.0?
MongoDB的界面和之前的界面完全不同了。
Here 您可以查看官方文档,其中包含一些有关如何搜索、插入和更新的示例,但是更新插入呢?
meta 的想法:我尝试在 google 和 SO 上搜索,但许多资源都引用旧界面。也许创建一个 MongoLegacy 标签会很好。
将 UpdateOptions
的实例作为 UpdateOneAsync(filter, update, options)
中的选项参数传递,例如:
collection.UpdateOneAsync(p => p.Id == user.Id,
Builders<User>.Update.Set(p => p.Name, "John"),
new UpdateOptions { IsUpsert = true });
编辑
要替换文档,请改为调用 ReplaceOneAsync
:
collection.ReplaceOneAsync(p => p.Id == user.Id,
user,
new ReplaceOptions { IsUpsert = true });
MongoDB的界面和之前的界面完全不同了。 Here 您可以查看官方文档,其中包含一些有关如何搜索、插入和更新的示例,但是更新插入呢?
meta 的想法:我尝试在 google 和 SO 上搜索,但许多资源都引用旧界面。也许创建一个 MongoLegacy 标签会很好。
将 UpdateOptions
的实例作为 UpdateOneAsync(filter, update, options)
中的选项参数传递,例如:
collection.UpdateOneAsync(p => p.Id == user.Id,
Builders<User>.Update.Set(p => p.Name, "John"),
new UpdateOptions { IsUpsert = true });
编辑
要替换文档,请改为调用 ReplaceOneAsync
:
collection.ReplaceOneAsync(p => p.Id == user.Id,
user,
new ReplaceOptions { IsUpsert = true });