mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 10:08:19 +08:00
Update mongodb
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Serializers;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public class DbDataReaderFactory
|
||||
{
|
||||
public DbDataReader Handle(string operation, IMongoCollection<BsonDocument> collection, string json)
|
||||
{
|
||||
var doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(json);
|
||||
IQueryHandler queryHandler = null;
|
||||
if (operation == "find")
|
||||
{
|
||||
queryHandler = new QueryFindHandler();
|
||||
}
|
||||
else if (operation == "aggregate")
|
||||
{
|
||||
queryHandler = new QueryAggregateHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException($" NotSupportedException: {operation} ");
|
||||
}
|
||||
return queryHandler.Find(collection, doc);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public interface IQueryHandler
|
||||
{
|
||||
DbDataReader Find(IMongoCollection<BsonDocument> collection, BsonValue doc);
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
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
|
||||
var cursor = aggregateFluent.ToList();
|
||||
return new MongoDbBsonDocumentDataReader(cursor);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public class QueryFindHandler : IQueryHandler
|
||||
{
|
||||
public DbDataReader Find(IMongoCollection<BsonDocument> collection, BsonValue doc)
|
||||
{
|
||||
BsonDocument filter;
|
||||
BsonDocument projection = null;
|
||||
|
||||
if (doc.IsBsonArray)
|
||||
{
|
||||
var array = doc.AsBsonArray;
|
||||
filter = array.Count > 0 ? array[0].AsBsonDocument : new BsonDocument();
|
||||
if (array.Count > 1)
|
||||
projection = array[1].AsBsonDocument;
|
||||
}
|
||||
else if (doc.IsBsonDocument)
|
||||
{
|
||||
filter = doc.AsBsonDocument;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid JSON format for MongoDB find operation.");
|
||||
}
|
||||
|
||||
var findFluent = collection.Find(filter);
|
||||
|
||||
if (projection != null)
|
||||
findFluent = findFluent.Project<BsonDocument>(projection);
|
||||
|
||||
var cursor = findFluent.ToCursor(); // 已包含 filter + projection 的结果
|
||||
|
||||
return new MongoDbIAsyncCursorDataReader(cursor); // 你要确保这个类支持逐行读取 BsonDocument
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user