mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-17 05:29:33 +08:00
37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MongoDb.Ado.data
|
|
{
|
|
public class UpdateManyHandlerAsync : IMongoOperationHandlerAsync
|
|
{
|
|
public string operation { get; set; }
|
|
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
|
|
{
|
|
var documents = ParseJsonArray(json);
|
|
int total = 0;
|
|
foreach (var doc in documents)
|
|
{
|
|
var filter = doc["filter"].AsBsonDocument;
|
|
var update = doc["update"].AsBsonDocument;
|
|
var result =await collection.UpdateManyAsync(filter, update);
|
|
total += (int)result.ModifiedCount;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
private List<BsonDocument> ParseJsonArray(string json)
|
|
{
|
|
if (json.TrimStart().StartsWith("["))
|
|
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
|
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
|
}
|
|
}
|
|
|
|
}
|