Search code examples
c#mongodbmongodb-.net-driver

Debug MongoDB queries with the C# driver v2 API


Can I serialize a FilterDefinition<T> to a json string to see what is being built under the hood? Or only via the logs and a more verbose database profiler setting?


Solution

  • Yes, FilterDefinition has a Render method on it. Provided you have access to the collection you are using, you can do the following.

    var renderedFilter = definition.Render(collection.DocumentSerializer, collection.Settings.SerializerRegistry);
    

    It will render to a BsonDocument, what you can use ToString() on that if you want the JSON.