2025-05-02 11:39:41 +08:00
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using System;
|
2025-05-02 14:30:13 +08:00
|
|
|
|
using System.Collections;
|
2025-05-02 11:39:41 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace MongoDb.Ado.data
|
|
|
|
|
{
|
|
|
|
|
public class QueryFindHandler : 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
|
|
|
|
{
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 17:35:57 +08:00
|
|
|
|
var findFluent =Context?.IsAnyServerSession==true? collection.Find(Context.ServerSession,filter): collection.Find(filter);
|
2025-05-02 11:39:41 +08:00
|
|
|
|
|
|
|
|
|
if (projection != null)
|
|
|
|
|
findFluent = findFluent.Project<BsonDocument>(projection);
|
|
|
|
|
|
2025-07-03 13:13:49 +08:00
|
|
|
|
var cursor = findFluent.ToList();
|
|
|
|
|
var result = MongoDbDataReaderHelper.ToDataReader(cursor);
|
|
|
|
|
return result;
|
2025-05-02 11:39:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|