Update Mongodb

This commit is contained in:
sunkaixuan
2025-05-02 07:54:16 +08:00
parent aca8693c5d
commit 5ec98af257
11 changed files with 215 additions and 84 deletions

View File

@@ -0,0 +1,34 @@
using MongoDB.Bson.Serialization;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
namespace MongoDb.Ado.data
{
public class UpdateManyHandler : IMongoOperationHandler
{
public int Handle(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 = collection.UpdateMany(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) };
}
}
}