Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 853
Benjamin E. Avatar asked Jul 31 '15 04:07

Benjamin E.


1 Answers

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.

like image 62
Craig Wilson Avatar answered Oct 04 '22 23:10

Craig Wilson