是否有“解释查询"?对于 MongoDB Linq? [英] Is there an "Explain Query" for MongoDB Linq?

查看:19
本文介绍了是否有“解释查询"?对于 MongoDB Linq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Linq 查询上运行 .explain() 或等价物?我想知道

Is there a way to run .explain() or equivalent on Linq queries? I would want to know

  • 实际 JSON 查询的文本
  • .explain() 的输出(使用的索引等)
  • 查询的执行时间也很好
  • The text of the actual JSON query
  • The output of .explain() (indexes used, etc)
  • It would also be nice to have the execution time of the query

推荐答案

如果你有一个查询包装器,你可以很容易地获得 Json;

You can get the Json easily enough if you have a query wrapper;

var qLinq = Query<T>.Where(x => x.name=="jim");
Console.WriteLine(qLinq.ToJson());

MongoCursor 上还有一个 Explain() 方法,所以你可以这样做;

There's also an Explain() method on MongoCursor, so you could do this;

var exp = Collection.FindAs<T>(qLinq).Explain()
Console.WriteLine(exp.ToJson());

所以如果你想花时间,millis"就在那里;

So if you want the time taken, "millis" is in there;

var msTaken = exp.First(x => x.Name == "millis").Value.AsInt32;

如果你有一个IQueryable,试试这样的;

If you have an IQueryable, try something like this;

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()

这篇关于是否有“解释查询"?对于 MongoDB Linq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆