是否可以在Linq查询上运行.explain()
或同等功能?我想知道
如果有查询包装器,您可以很容易地获得Json;
.explain()
[MongoCursor上也有一个Explain()方法,所以您可以这样做;
var qLinq = Query<T>.Where(x => x.name=="jim");
Console.WriteLine(qLinq.ToJson());
所以,如果您想花点时间,“ millis”就在里面;
var exp = Collection.FindAs<T>(qLinq).Explain()
Console.WriteLine(exp.ToJson());
如果您有var msTaken = exp.First(x => x.Name == "millis").Value.AsInt32;
,请尝试类似的事情;
IQueryable
如果您想在库中使用此功能,我刚刚创建了一个名为[]的GitHub项目
。NET的MongoDB查询助手
void Do(MongoCollection col, IQueryable iq)
{
// Json Mongo Query
var imq = (iq as MongoQueryable<Blob>).GetMongoQuery();
Console.WriteLine(imq.ToString());
// you could also just do;
// var cursor = col.FindAs(typeof(Blob), imq);
var cursor = MongoCursor.Create(typeof(Blob), col, imq, ReadPreference.Nearest);
var explainDoc = cursor.Explain();
Console.WriteLine(explainDoc);
}//Do()
它将:
检查出来,如果发现有趣的话,则做出贡献。
是,有。它显示https://github.com/mikeckennedy/mongodb-query-helper-for-dotnet的所有操作,并具有一个布尔值(包括执行时间):
.explain
虽然不显示查询。这是一个扩展方法:
var database = new MongoClient().GetServer().GetDatabase("db");
var collection = database.GetCollection<Hamster>("Hamsters");
var explanation = collection.AsQueryable().Where(hamster => hamster.Name == "bar").Explain(true);
Console.WriteLine(explanation);
用法:
public static string GetMongoQuery<TItem>(this IQueryable<TItem> query)
{
var mongoQuery = query as MongoQueryable<TItem>;
return mongoQuery == null ? null : mongoQuery.GetMongoQuery().ToString();
}
在mongodb 3 C#中,我使用了以下内容:
var query = collection.AsQueryable().Where(hamster => hamster.Name == "bar").GetMongoQuery();
Console.WriteLine(query);
结果:
var users = Mongo.db.GetCollection<User>("Users");
var r = users(m => m._id == yourIdHere)
.Project(m => new { m._id, m.UserName, m.FirstName, m.LastName })
.Limit(1);
Console.WriteLine(r.ToString());