mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-26 22:25:49 +08:00
Update mongodb
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Serializers;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public class DbDataReaderFactoryAsync
|
||||
{
|
||||
public async Task<DbDataReader> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json)
|
||||
{
|
||||
var doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(json);
|
||||
IQueryHandlerAsync queryHandler = null;
|
||||
if (operation == "find")
|
||||
{
|
||||
queryHandler = new QueryFindHandlerAsync();
|
||||
}
|
||||
else if (operation == "aggregate")
|
||||
{
|
||||
queryHandler = new QueryAggregateHandlerAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await ExecuteHandlerFactoryAsync.HandlerAsync(operation,json, collection);
|
||||
return new DataTable().CreateDataReader();
|
||||
}
|
||||
return await queryHandler.HandlerAsync(collection, doc);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System.Data.Common;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public interface IQueryHandlerAsync
|
||||
{
|
||||
Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public class QueryAggregateHandlerAsync : IQueryHandlerAsync
|
||||
{
|
||||
public async Task<DbDataReader> HandlerAsync(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 =await aggregateFluent.ToListAsync();
|
||||
return new MongoDbBsonDocumentDataReader(cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDb.Ado.data
|
||||
{
|
||||
public class QueryFindHandlerAsync : IQueryHandlerAsync
|
||||
{
|
||||
public async Task<DbDataReader> HandlerAsync(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 =await findFluent.ToListAsync();
|
||||
return new MongoDbBsonDocumentDataReader(cursor); // 你要确保这个类支持逐行读取 BsonDocument
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user