很久以前使用旧版本的 Mongo C# 驱动程序,可以按照这个思路做一些事情(我完全忘记了)。
collection.FindOne(query).Explain()
这将使用 cursor.explain() 提供查询执行、使用的索引等详细信息.现在我们正在使用 2.4 并希望对我们的一个查询使用解释。
我发现与此相关的唯一问题是 this one但是使用的驱动程序似乎又不同了。
如何使用 2.4 C# 驱动程序运行解释查询?
最佳答案
根据 issue on MongoDB's 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. As a result, we've not included explain as part of the API.
幸运的是,仍然可以通过在 FindOptions
中提供查询修饰符:
var options = new FindOptions
{
Modifiers = new BsonDocument("$explain", true)
};
var explain = await collection.Find(x => true, options)
.Project(new BsonDocument())
.FirstOrDefault()
?.ToJson();
只需将 x => true
替换为您想要分析的查询即可。我添加了 .ToJson()
以获得人类可读的 JSON 字符串。
关于c# - 如何使用 2.4 C# Mongo 驱动程序运行解释查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49506857/