[英]How do I run an explain query with the 2.4 C# Mongo driver?
A long time ago using an older version of the Mongo C# driver, it was possible to do something along the lines of this (I forget exactly). 很久以前使用较旧版本的Mongo C#驱动程序,就可以做到这一点(我完全忘了)。
collection.FindOne(query).Explain()
And this would provide details of query execution, indexes used, etc. using cursor.explain() . 这将使用cursor.explain()提供查询执行,使用的索引等的详细信息。 Now we're using 2.4 and would like to use explain for one of our queries.
现在我们正在使用2.4,并希望对我们的一个查询使用explain。
The only question I found relating to this was this one but the driver used seems to be different again. 我发现与此有关的唯一问题是这个问题,但使用的驱动程序似乎又有所不同。
How do I run an explain query with the 2.4 C# driver? 如何使用2.4 C#驱动程序运行解释查询?
According to an issue on MongoDB's JIRA page, it was removed from the API as an out of the box feature. 根据MongoDB的JIRA页面上的一个问题 ,它已作为开箱即用的功能从API中删除。
Explain is/has undergone some changes and adding it to the driver before that is done would have been a mistake.
解释是/经历了一些更改并在完成之前将其添加到驱动程序将是一个错误。 In addition, we feel that most explanations happen in the shell and not in the drivers.
此外,我们认为大多数解释都发生在shell而不是驱动程序中。 As a result, we've not included explain as part of the API.
因此,我们没有将解释作为API的一部分。
Fortunately, it's still possible by supplying the query modifier in FindOptions
: 幸运的是,通过在
FindOptions
提供查询修饰符仍然可以:
var options = new FindOptions
{
Modifiers = new BsonDocument("$explain", true)
};
var explain = await collection.Find(x => true, options)
.Project(new BsonDocument())
.FirstOrDefault()
?.ToJson();
Simply replace x => true
with the query you would like to profile. 只需将
x => true
替换为您要分析的查询即可。 I've added .ToJson()
in order to get a nice human-readable JSON string. 我添加了
.ToJson()
以获得一个很好的人类可读的JSON字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.