驱动名称:
MongoDB.Driver
重要特性:
public async Task BatchSave<T>(string collectionName, List<T> entities) where T : BsonDocument
{
var collection = _database.GetCollection<T>(collectionName);
var models = new List<WriteModel<T>>();
foreach (var item in entities)
{
if (!item.Contains("_id"))
{
continue;
}
var filter = Builders<T>.Filter.Eq("_id", item.GetValue("_id"));
models.Add(new ReplaceOneModel<T>(filter, item) { IsUpsert = true });
}
await collection.BulkWriteAsync(models, new BulkWriteOptions { IsOrdered = false });
}