运行我的应用程序时,我必须写入屏幕原始查询。如何在运行fluent-mongo时获得“真正的”mongo查询
任何可用的方法/扩展方法,从这个获得:
db.EventsReceiver.find({ "userid" : "123" });
运行我的应用程序时,我必须写入屏幕原始查询。如何在运行fluent-mongo时获得“真正的”mongo查询
任何可用的方法/扩展方法,从这个获得:
db.EventsReceiver.find({ "userid" : "123" });
对于有同样的问题,我会在这里重新发布在github上克雷格的回答人:
IQueryable alldata = hr.GetCollection"EventsReceiver").AsQueryable().Where(q => q.UserId == "123");
类似于:
var queryObject = ((IMongoQueryable)alldata).GetQueryObject();
这应该会让您回到用于生成查询的对象。
从FluentMongo v1.2.0.0开始,没有公开的方式来公开查询(很伤心)。这是一个肮脏的扩展方法来解决它。
但是由于这是使用反射来获取非公众成员,所以不要指望它将来一定会有效。
public static class MongoQueryableExtensions
{
public static BsonDocument GetMongoQuery<T>(this IQueryable<T> query)
{
if(query == null) throw new ArgumentNullException("query");
Assembly fluentMongoAssembly = typeof(FluentMongo.Linq.MongoCollectionExtensions).Assembly;
Type mongoQueryableType = fluentMongoAssembly.GetType("FluentMongo.Linq.IMongoQueryable");
BsonDocument queryDocument = null;
if(mongoQueryableType.IsAssignableFrom(query.GetType()))
{
MethodInfo m = mongoQueryableType.GetMethod("GetQueryObject");
object queryObject = m.Invoke(query, null);
PropertyInfo queryProperty = fluentMongoAssembly.GetType("FluentMongo.Linq.MongoQueryObject").GetProperty("Query");
queryDocument = (BsonDocument)queryProperty.GetValue(queryObject, null);
}
return queryDocument;
}
}
是不是这样的呢? http://stackoverflow.com/questions/10261156/translate-queryablet-back-to-imongoquery?lq=1 –
检查http://stackoverflow.com/questions/10261156/translate-queryablet-back-to-imongoquery?lq=1 –