5

I'm trying to sort the release versions in form of "a.b.c"

I'm using mongo-java-driver

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.8.0</version>
</dependency>

I have created the index with collation:

{
    "v" : 2,
    "key" : {
            "version" : 1
    },
    "name" : "version_1",
    "ns" : "db.sysversion",
    "collation" : {
            "locale" : "en",
            "caseLevel" : false,
            "caseFirst" : "off",
            "strength" : 3,
            "numericOrdering" : true,
            "alternate" : "non-ignorable",
            "maxVariable" : "punct",
            "normalization" : false,
            "backwards" : false,
            "version" : "57.1"
    }
}

I have implemented the aggregation query with java driver:

Collation collation = Collation.builder().locale("en").numericOrdering(true).build();

ArrayList<Document> response = new ArrayList<>();

ArrayList<Bson> aggregate = new ArrayList<Bson>(Arrays.asList(
  match(gt("version", "1.9.4")), sort(descending("version")),
  project(fields(include("version"), exclude("_id")))
));

this.database.getCollection(sysversion).aggregate(aggregate).collation(collation).into(response);

And I'm returning the list in a document as API response.

return new Document("version", response);

But the output I'm getting is:

{ "version" : [{ "version" : "\u000f\u0003\b\u000f\f\b\u000f\u0003\u0001\t\u0001\t" }, { "version" : "\u000f\u0003\b\u000f\f\b\u000f\u0002\u0001\t\u0001\t" }] }

And when I tried the same with Mongo shell I get the following output (which is correct)

{
  version:[
    {
    "version" : "1.10.1"
    },
    {
    "version" : "1.10.0"
    }
  ]
}

What's wrong with my Java code? is it the version or error in code?

Any help would be much appreciated.

Vijay Rajpurohit
  • 1,266
  • 2
  • 13
  • 25
  • Try this in your `response.forEach(System.out::println);`: instead of `System.out::println` use `doc -> System.out.println(doc.toJson())`. – prasad_ May 04 '20 at 12:14
  • No, This is the API response taken from the postman. So, the issue is not with printing. – Vijay Rajpurohit May 04 '20 at 12:19
  • 1
    MongoDB stores strings in UTF-8, that incorrect output looks kind of like unicode. – Joe May 12 '20 at 08:13
  • True @Joe, it is returning Unicode value but I'm getting correct output in shell query. Facing this issue with Mongo-Java-Driver. – Vijay Rajpurohit May 12 '20 at 12:56

1 Answers1

2

Found the issue

I debugged the issue found that the collation uses the normalization for encoding the query response. And by default, that value is false. So, the shell query was returning the correct output.

But In Mongo-java-Driver it was setting normalization as true(by default).

Updated the builder with normalization as false for same:

Collation collation = Collation.builder().locale("en").numericOrdering(true).normalization(false).build();
ArrayList<Document> response = new ArrayList<>();

ArrayList<Bson> aggregate = new ArrayList<Bson>(Arrays.asList(
  match(gt("version", "1.9.4")), sort(descending("version")),
  project(fields(include("version"), exclude("_id")))
));

this.database.getCollection(sysversion).aggregate(aggregate).collation(collation).into(response);

This fixed my issue.

Vijay Rajpurohit
  • 1,266
  • 2
  • 13
  • 25