title: Bson pretty print using Java MongoDb driver
body: <p>I am using the Mongo Aggregation Framework using the Java MongoDB driver, version 3.3. I have an <em>aggregagtion pipeline</em>, that is merely collection of type <code>List&lt;Bson&gt;</code>. I am trying to find a way to pretty print each stage of the pipeline.</p>
  
  <p>Calling the <code>toString</code> method on each element is not sufficient, because each stages is an instance of a simple implementation of the <code>Bson</code> interface, which is <code>SimplePipelineStage</code>. This stupid class has not any override of the <code>toString</code> method.</p>
  
  <p>The pipeline is created using factory methods of mongo java driver <code>Aggregates</code> class, like the following:</p>
  
  <pre><code>Aggregates.match(/* ... */)
  Aggregates.project(/* ... */)
  // And so on...
  </code></pre>
  
  <p>Javadoc can be found <a href="http://api.mongodb.com/java/3.3/?com/mongodb/client/model/Aggregates.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>How can I pretty print such objects? I know for sure that the type <code>BasicDbObject</code> has a smart <code>toString</code> implementation, but I cannot find a way to convert from <code>Bson</code> to <code>BasicDbObject</code>.</p>
  
  <p>Thanks a lot in advance.</p>