简体   繁体   English

在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?

The answers MongoDB - can't use .explain() to get query info in C# code? 答案MongoDB-无法使用.explain()获取C#代码中的查询信息? or Is there an "Explain Query" for MongoDB Linq? 对MongoDB Linq是否有“解释查询”? are outdated and the classes and methods doesn't exist anymore in recent versions of the C# driver. 已过时,并且在最新版本的C#驱动程序中不再存在类和方法。

As a workaround I was able to use the find syntax with options overload to run explain on the queries I wanted to tune. 作为一种解决方法,我可以使用带有选项重载的find语法在要调整的查询上运行解释。

    [Fact]
    public async Task Metrics()
    {
        var options = new FindOptions
        {
            Modifiers = new BsonDocument("$explain", true)
        };
        var queryable = await MongoDb.Collection.Find("{ Paid: false}", options).As<StatsBsonDocument>().ToListAsync(); // StatsBsonDocument BsonDocument  
        //Console.WriteLine(queryable.First());
        queryable.First().GetStats();            
    }



public class StatsBsonDocument
{
    public dynamic queryPlanner { get; set; }
    public ExecutionStats executionStats { get; set; }
    public dynamic serverInfo { get; set; }

    public void GetStats()
    {
        try
        {
            Console.WriteLine($"{queryPlanner.winningPlan.inputStage.stage}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"executionSuccess {executionStats.executionSuccess}, stage {queryPlanner.winningPlan.stage},");
        }
        Console.WriteLine($"{executionStats.nReturned} returned documents, {executionStats.executionTimeMillis}ms execution");
        Console.WriteLine($"{executionStats.totalKeysExamined} keys examined , {executionStats.totalDocsExamined} documents examined");
        Console.WriteLine($"index vs returned {(decimal)executionStats.totalKeysExamined / executionStats.nReturned}, " +
        $"scanned documents/returned {(decimal)executionStats.totalDocsExamined / executionStats.nReturned}");
        Console.WriteLine();

        // AND_SORTED stage or an AND_HASH stage. index intersection
    }
}

public class QueryPlanner
{
    public dynamic plannerVersion;
    public string Namespace;
    public dynamic indexFilterSet;
    public dynamic parsedQuery;
    public dynamic winningPlan;
    public dynamic rejectedPlans;
}

public class ExecutionStats
{
    public bool executionSuccess;
    public int nReturned;
    public int executionTimeMillis;
    public int totalKeysExamined;
    public int totalDocsExamined;
    public dynamic executionStages;
    public dynamic allPlansExecution;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongo DB C#驱动程序-如何在集合中通过ID加入? - Mongo db c# driver - how to join by id in collection? 如何在c#驱动程序中为mongo集合中的所有文本字段创建全文搜索? - How do you create a full text search for all text fields in a mongo collection in the c# driver? 如何使用mongo db c#驱动程序执行“和”查询? - How to do “And” queries using mongo db c# driver? 如何使用2.4 C#Mongo驱动程序运行解释查询? - How do I run an explain query with the 2.4 C# Mongo driver? 如何通过一次调用使用mongo db C#驱动程序添加嵌套元素或更新属性 - How can you add a nested element or update a property using mongo db C# driver with one call 如何在当前的 mongodb c# 驱动程序版本中执行解释? - How do I perform explain in current mongodb c# driver version? 如何通过 DateTime 的 C# 驱动程序在 Mongo 集合中查找条目? - How to find an entry in a Mongo Collection through C# driver by DateTime? 如何使用C#驱动程序针对mongo设置单个查询的readPreference - How do you set the readPreference for a single query against mongo using the c# driver 如何在C#mongo驱动程序中设置复数集合名称? - How to set plural collection name in C# mongo driver? 如何用C#驱动程序填充mongo Db表? - How to fill mongo Db table by C# Driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM