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

标签 c# mongodb

很久以前使用旧版本的 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/

相关文章:

java - Mongo Jackson Mapper 按 id 结果删除

java - Spring + Mongo + 泛型 + 灵活性

node.js - 在 Mongo 中,增量和返回值——可能在 1 个调用中?

c# - 兼容 php、java 和 c# 的图像编码/解码方式

c# - 求解方程从3个点找到圆心

c# - 如何在1秒钟内调用函数200次

javascript - 使用 elemMatch 从数组 Node.js 返回一个对象

c# - 如何查询正在运行的线程

c# - 如何在 C# 中使用 XUnit 正确模拟 mongoDb?

mongodb - 如何将整个 MongoDB 数据库转储为 text/json?