简体   繁体   English

如何使用2.4 C#Mongo驱动程序运行解释查询?

[英]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.

相关问题 如何在 mongo 中使用 C# .net 运行自定义查询? - How do I run a custom query with C# .net in mongo? 在Mongo Server 4.0上的C#驱动程序2.7.0中如何执行db.collection.explain()? - How do you perform db.collection.explain() in C# driver 2.7.0 on Mongo Server 4.0? 如何使用 Mongo.Driver.Linq 和 Mongo C# 驱动程序 2.3 返回带有过滤子文档的文档? - How do I return a document with filtered sub-documents using Mongo.Driver.Linq with the Mongo C# driver 2.3? 如何使用C#驱动程序针对mongo设置单个查询的readPreference - How do you set the readPreference for a single query against mongo using the c# driver 如何在当前的 mongodb c# 驱动程序版本中执行解释? - How do I perform explain in current mongodb c# driver version? 如何使用Linq查询Mongo C#2.2驱动程序中的嵌套列表? - How to query nested list in Mongo C# 2.2 driver with Linq? 如何在Mongo驱动程序中为“orderby”编写查询以进行排序? - How to write a query for “orderby” in Mongo driver for C# to sort? 如何在 C# Mongo Driver 中使用 $facet 进行以下查询? - How to use $facet in C# Mongo Driver for following query? 如何将此控制台mongodb查询转换为C#mongo驱动程序v2? - How can I convert this console mongodb query to a C# mongo driver v2 one? 如何使用mongo db c#驱动程序执行“和”查询? - How to do “And” queries using mongo db c# driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM