mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
31 lines
970 B
C#
31 lines
970 B
C#
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
|
|
namespace MongoDb.Ado.data
|
|
{
|
|
public class InsertManyHandlerAsync : IMongoOperationHandlerAsync
|
|
{
|
|
public CancellationToken token { get; set; }
|
|
public string operation { get; set; }
|
|
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
|
|
{
|
|
var documents = ParseJsonArray(json);
|
|
await collection.InsertManyAsync(documents,null,token);
|
|
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) };
|
|
}
|
|
}
|
|
}
|