如何使用C#驱动程序在MongoDB中更新和更新多个文档 [英] How to update and upsert multiple documents in MongoDB using C# Drivers
本文介绍了如何使用C#驱动程序在MongoDB中更新和更新多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MongoDB 2,我想更新多个文档并向上插入一个processed:true
之类的值
进入收藏.但是MongoDB c#API仅允许我们更新多个记录或更新单个记录.
I am using MongoDB 2, and I want to update multiple documents and upsert a value like processed:true
into the collection. But MongoDB c# api only allows us to either Update Multiple Records or Upsert a single record.
如何使用C#API解决此问题?
How to solve this problem using the C# api?
推荐答案
After Mongo 2.6
you can do Bulk Updates/Upserts. Example below does bulk update using c#
driver.
MongoCollection<foo> collection = database.GetCollection<foo>(collectionName);
var bulk = collection.InitializeUnorderedBulkOperation();
foreach (FooDoc fooDoc in fooDocsList)
{
var update = new UpdateDocument { {fooDoc.ToBsonDocument() } };
bulk.Find(Query.EQ("_id", fooDoc.Id)).Upsert().UpdateOne(update);
}
BulkWriteResult bwr = bulk.Execute();
这篇关于如何使用C#驱动程序在MongoDB中更新和更新多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文