I am using the Mongo Aggregation Framework using the Java MongoDB driver, version 3.3. I have an aggregagtion pipeline, that is merely collection of type List<Bson>
. I am trying to find a way to pretty print each stage of the pipeline.
Calling the toString
method on each element is not sufficient, because each stages is an instance of a simple implementation of the Bson
interface, which is SimplePipelineStage
. This stupid class has not any override of the toString
method.
The pipeline is created using factory methods of mongo java driver Aggregates
class, like the following:
Aggregates.match(/* ... */)
Aggregates.project(/* ... */)
// And so on...
Javadoc can be found here.
How can I pretty print such objects? I know for sure that the type BasicDbObject
has a smart toString
implementation, but I cannot find a way to convert from Bson
to BasicDbObject
.
Thanks a lot in advance.
Googling a bit harder, I found a solution to pretty print a
Bson
instance. The trick is to convert it into an instance ofBsonDocument
, which has an implementation of thetoString
method that returns the string representation of the corresponding JSON.The original link is the following: Converting Bson object to BsonDocument.