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.