Sunday, 21 January 2018

MongoDB c# driver debugging

With the older versions of MongoDb c# drivers it was easy to convert to string any Mongo Update or Query generated using the driver. But with the introduction of the MongoDB.Driver.Builders<T> this useful functionality when debugging or troubleshooting is not as straightforward as before, but researching a bit I was able to found how to get the Bson representation of the query/update:

public static class MongoBuildersTestingExtensions
{
public static string RenderToString<T>(this UpdateDefinition<T> updateDefinition)
    {
var serializerRegistry = BsonSerializer.SerializerRegistry;
var documentSerializer = serializerRegistry.GetSerializer<T>();
var rendered = updateDefinition.Render(documentSerializer, serializerRegistry);
return rendered.ToString();
}
}

No comments:

Post a Comment