Get generated script MongoDB C # driver

I am using MongoDB.Driver 2.0.0. Is there a way to see the generated script from linq in MongoDB?

For example, my request looks like this:

IFindFluent<ProductMapping, ProductMapping> findFluent = Collection.Find(
    x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId);

      

How would this (or more complex queries) be represented in the MongoDB shell?

+3


source to share


2 answers


EDIT

Refer to i3arnon's answer for a client side method using Render()

which is usually simpler.




You can use the mongodb integrated profiler to see what the database actually got:

db.setProfilingLevel(2); // log every request

// show the requests that mongodb has received, along with execution stats:
db.system.profile.find().pretty() 

      

Alternatively, you can go into the driver source code and wait for it to actually create the message. This, however, requires compiling the driver from the source AFAIK.

+2


source


EDIT: As of version 2.0.1 of the driver, the object FindFluent

returned from IMongoCollection.Find

has a matching one ToString

, which includes a filter as well as projection, sorting, etc. (if necessary).

So for this:

var findFluent = collection.
    Find(x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId,
        new FindOptions {MaxTime = TimeSpan.FromSeconds(1)}).
    Project(x => x.UrlHash).
    Sort(Builders<ProductMapping>.Sort.Descending(x => x.ProductTopic)).
    Skip(6).
    Limit(7);

Console.WriteLine(findFluent);

      

The output would be:

find({ "UrlHash" : { "$in" : [4, 5, 6, 7, 8] }, "ProductTopic" : 200 }, { "UrlHash" : 1, "_id" : 0 }).
sort({ "ProductTopic" : -1 }).
skip(6).
limit(7).
maxTime(1000)

      




Okay, you already know what you are doing the search, so I assume you want to know what the query looks like.

You can easily do this directly from your code using IFindFluent.Filter

:

BsonDocument filterDocument = findFluent.Filter.Render(
    collection.DocumentSerializer,
    collection.Settings.SerializerRegistry);

Console.WriteLine(filterDocument);

      

The output in your case (depends on hashValues

and of topicId

course):

{ "UrlHash" : { "$in" : [4, 5, 6, 7, 8, 9] }, "ProductTopic" : 200 }

      

+8


source







All Articles