简体   繁体   中英

How do I perform explain in current mongodb c# driver version?

I'm just starting with MongoDb driver and I've found multiple posts on Stackoverflow, that say just add .Explain() to your IQueryable and it will do the magic. Problem is, that my IDE does not suggest such method.

I've started digging and I see in master branch of MongoDb driver, that Explain extension method is in LinqExtensionMethods class, which itself is in MongoDB.Driver.Legacy, which is not referenced from the current driver?

So what is the approach with current driver?

The only way to do this is by hacking around everything being internal or private inside the MongoDB Driver which I wouldn't advise you doing, however, it's possible with the below code.

var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection<Event>("events");

var filter = Builders<Event>.Filter.Gte(x => x.At, DateTime.UtcNow);
var fields = Builders<Event>.Projection.Include(x => x.Name);

var settingsSerializerRegistry = collection.Settings.SerializerRegistry;
var bsonSerializer = settingsSerializerRegistry.GetSerializer<Event>();
var command = new BsonDocument
{
    { "find", collection.CollectionNamespace.CollectionName },
    { "filter", filter.Render(bsonSerializer, settingsSerializerRegistry)},
    { "projection", fields.Render(bsonSerializer, settingsSerializerRegistry) },
    { "skip", 5 },
    { "limit", 10 },
};

var subject = new ExplainOperation(database.DatabaseNamespace, command, new MessageEncoderSettings())
{
    Verbosity = ExplainVerbosity.QueryPlanner
};
var readPreferenceBinding = new ReadPreferenceBinding(client.Cluster, ReadPreference.Primary, NoCoreSession.NewHandle());

var explain = subject.Execute(readPreferenceBinding, CancellationToken.None);
//{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.events", "indexFilterSet" : false, "parsedQuery" : { "At" : { "$gte" : ISODate("2020-03-14T23:45:37.143Z") } }, "winningPlan" : { "stage" : "EOF" }, "rejectedPlans" : [] }, "serverInfo" : { "host" : "DESKTOP-ULTR09L", "port" : 27017, "version" : "4.2.3", "gitVersion" : "6874650b362138df74be53d366bbefc321ea32d4" }, "ok" : 1.0 }

However, the explain extensions are in the MongoDB.Driver.Legacy package, this still is supported for anything that can run .NETStandard 1.5 and .NETFramework 4.5.2

So this might be a better route for you! 😍

You can install it from the package manager

Install-Package mongocsharpdriver

Then you can use the following extensions to call explain on a query:

var client = new MongoClient();

var database = client.GetDatabase("test");
var collection = database.GetCollection<Event>("events");
var explain = collection.AsQueryable()
    .Where(c => c.At >= DateTime.UtcNow.AddDays(-1) && c.At < DateTime.UtcNow)
    .Take(1)
    .Explain();

It's a lot more simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM