Update mongodb

This commit is contained in:
sunkaixuan
2025-05-02 16:30:22 +08:00
parent dc0e1af9f8
commit 6d854a492e
15 changed files with 396 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
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 InsertManyHandlerAsync : IMongoOperationHandlerAsync
{
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);
await collection.InsertManyAsync(documents);
return documents.Count;
}
private List<BsonDocument> ParseJsonArray(string json)
{
if (json.TrimStart().StartsWith("["))
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
return new List<BsonDocument> { BsonDocument.Parse(json) };
}
}
}