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

28 lines
855 B
C#
Raw Normal View History

2025-05-02 11:39:41 +08:00
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
namespace MongoDb.Ado.data
{
public class QueryAggregateHandler : IQueryHandler
{
public DbDataReader Find(IMongoCollection<BsonDocument> collection, BsonValue doc)
{
// 解析 JSON 字符串为 BsonArray
var pipeline = doc.AsBsonArray; ;
// 构建聚合管道
var aggregateFluent = collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray());
// 执行聚合查询并返回 DbDataReader
2025-05-02 14:30:13 +08:00
var cursor = aggregateFluent.ToEnumerable();
2025-05-02 11:39:41 +08:00
return new MongoDbBsonDocumentDataReader(cursor);
}
}
}