I was trying to solve the same problem today. Here is what I found.
public static class MongoExtensions
{
public static BsonDocument RenderToBsonDocument<T>(this FilterDefinition<T> filter)
{
var serializerRegistry = BsonSerializer.SerializerRegistry;
var documentSerializer = serializerRegistry.GetSerializer<T>();
return filter.Render(documentSerializer, serializerRegistry);
}
}
I didn't have access to a collection when I was calling it, so I couldn't use the above solutions.
This allows you to do
var json = filter.RenderToBsonDocument().ToJson();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…