4

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.

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
  • `.toJson()` It's actually a method – Neil Lunn Aug 31 '17 at 09:15
  • `toJson` is not define on `Bson` type. Please, read the question, first – riccardo.cardin Aug 31 '17 at 09:15
  • Sorry was thinking of `Document`, which you really should be using anyway. I personally pump pipeline debugging through Gson. So I could find a code block somewhere. – Neil Lunn Aug 31 '17 at 09:17
  • I am using `Aggregates` factory methods to create each pipelines. It is not my choice to use an abstract `Bson` :) – riccardo.cardin Aug 31 '17 at 09:19
  • Can you show the actual code constructing the pipeline please. It would be useful to see exactly what you are using so you can be pointed to the correct place. – Neil Lunn Aug 31 '17 at 09:35
  • Nothing special, but I updated my question. Thanks. – riccardo.cardin Aug 31 '17 at 09:53
  • What do you actually mean by that? Something like `match = Aggregates.match(/* ... */)` or directly into a list `pipeline = Arrays.asList(Aggregates.match(/* ... */), Aggregates.project(/* ... */))` ? It's kind of why you where asked to show an actual block of code, so we can see the actual context. You aren't really telling us much in your question. – Neil Lunn Aug 31 '17 at 09:55
  • I found a solution: have a look to [this](https://groups.google.com/forum/#!topic/mongodb-user/peeL8wIWxwA) link – riccardo.cardin Aug 31 '17 at 10:08

4 Answers4

8

Googling a bit harder, I found a solution to pretty print a Bson instance. The trick is to convert it into an instance of BsonDocument, which has an implementation of the toString method that returns the string representation of the corresponding JSON.

Bson bson = Filters.gt("a", 10);
BsonDocument bsonDocument = bson.toBsonDocument(BsonDocument.class, MongoClient.DEFAULT_CODEC_REGISTRY);
System.out.println(bsonDocument);

The original link is the following: Converting Bson object to BsonDocument.

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
4

This is a rather old question, however I put my suggestion (for mongodb-driver 3.6.4) here as this is the most relevant post when googling on "mongodb java driver pretty print":

BsonDocument bsonDocument = bson.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry());
JsonWriterSettings.Builder settingsBuilder = JsonWriterSettings.builder().indent(true);
System.out.println(bsonDocument.toJson(settingsBuilder.build());
mimohodom
  • 41
  • 3
2

With the mongo 4 driver it looks something like this:

    private String pretty(Document document) {
        var settings = JsonWriterSettings.builder()
                .indent(true)
                .outputMode(JsonMode.SHELL)
                .build();
        return document.toJson(settings);
    }
Matthew
  • 10,361
  • 5
  • 42
  • 54
1

For mongodb-java API 3.4, the constant MongoClient.DEFAULT_CODEC_REGISTRY is no more accessible directly, it's a private member. There is a static method CodecRegistry getDefaultCodecRegistry() which returns the same constant.

Another point, BsonDocument.toString() internally does a toJson() with default JsonWriterSettings. In order to see the Shell equivalent of Query, use it like below:

public void logQuery(Bson filter) {
    if (LOGGER.isDebugEnabled()) {

        LOGGER.debug(
                "filter query: " + filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry())
                        .toJson(new JsonWriterSettings(JsonMode.SHELL)));
    }
}