mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-20 09:28:53 +08:00
BulkWriteHandlerAsync
This commit is contained in:
parent
0c9a7d2b41
commit
69d00e1213
@ -18,7 +18,17 @@ namespace MongoDb.Ado.data
|
||||
{
|
||||
var documents = ParseJsonArray(json);
|
||||
var bulkOps = new List<WriteModel<BsonDocument>>();
|
||||
|
||||
// 新增逻辑:如果只有一个文档且update只包含$set
|
||||
if (documents.Count == 1)
|
||||
{
|
||||
var doc = documents[0];
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
var update = doc["update"].AsBsonDocument;
|
||||
if (IsUpateBySql(update))
|
||||
{
|
||||
return await HandlePipelineUpdate(collection, filter, update);
|
||||
}
|
||||
}
|
||||
foreach (var doc in documents)
|
||||
{
|
||||
var filter = doc["filter"].AsBsonDocument;
|
||||
@ -46,6 +56,39 @@ namespace MongoDb.Ado.data
|
||||
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
|
||||
return new List<BsonDocument> { BsonDocument.Parse(json) };
|
||||
}
|
||||
|
||||
private static async Task<int> HandlePipelineUpdate(IMongoCollection<BsonDocument> collection, BsonDocument filter, BsonDocument update)
|
||||
{
|
||||
// 构造pipeline update
|
||||
// 构造pipeline update,不写死,循环现有的$set值
|
||||
var setDoc = update["$set"].AsBsonDocument;
|
||||
var setPipelineDoc = new BsonDocument();
|
||||
foreach (var element in setDoc.Elements)
|
||||
{
|
||||
// 检查值是否为BsonDocument且包含操作符(如$add),否则直接赋值
|
||||
if (element.Value.IsBsonDocument && element.Value.AsBsonDocument.GetElement(0).Name.StartsWith("$"))
|
||||
{
|
||||
setPipelineDoc[element.Name] = element.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
setPipelineDoc[element.Name] = element.Value;
|
||||
}
|
||||
}
|
||||
var updatePipeline = new[]
|
||||
{
|
||||
new BsonDocument("$set", setPipelineDoc)
|
||||
};
|
||||
var pipelineUpdate = new PipelineUpdateDefinition<BsonDocument>(updatePipeline);
|
||||
|
||||
var result =await collection.UpdateManyAsync(filter, pipelineUpdate);
|
||||
return (int)result.ModifiedCount;
|
||||
}
|
||||
|
||||
private static bool IsUpateBySql(BsonDocument update)
|
||||
{
|
||||
return update.ElementCount == 1 && update.Contains("$set");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user