Files
SqlSugar/Src/Asp.NetCore2/MongoDb.Ado.data/ExecuteDbDataReaderItems/QueryAggregateHandler.cs

33 lines
1.1 KiB
C#
Raw Normal View History

2025-05-02 11:39:41 +08:00
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
2025-05-05 14:26:49 +08:00
using MongoDB.Driver.Linq;
2025-05-02 11:39:41 +08:00
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
namespace MongoDb.Ado.data
{
public class QueryAggregateHandler : IQueryHandler
{
2025-07-11 17:35:57 +08:00
public HandlerContext Context { get; set; }
2025-05-02 15:50:20 +08:00
public DbDataReader Handler(IMongoCollection<BsonDocument> collection, BsonValue doc)
2025-05-02 11:39:41 +08:00
{
// 解析 JSON 字符串为 BsonArray
var pipeline = doc.AsBsonArray; ;
// 构建聚合管道
2025-07-11 17:35:57 +08:00
var aggregateFluent = Context?.IsAnyServerSession == true ?
collection.Aggregate<BsonDocument>(Context.ServerSession,pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray()):
collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray());
2025-05-02 11:39:41 +08:00
// 执行聚合查询并返回 DbDataReader
2025-05-05 14:26:49 +08:00
var cursor = aggregateFluent.ToList();
2025-07-03 13:13:49 +08:00
var result= MongoDbDataReaderHelper.ToDataReader(cursor);
2025-05-05 14:26:49 +08:00
return result;
2025-05-02 11:39:41 +08:00
}
}
}