英文:
Getting unwanted output from MongoDB collation
问题
以下是翻译好的部分:
我正在尝试对发布版本进行"a.b.c"形式的排序
我正在使用mongo-java-driver
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.0</version>
</dependency>
我已经创建了带有排序规则的索引:
{
"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"
}
}
我已经使用Java驱动程序实现了聚合查询:
Collation collation = Collation.builder().locale("en").numericOrdering(true).build();
ArrayList<Document> response = new ArrayList<>();
ArrayList<Bson> aggregate = new ArrayList<>(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);
并且我将列表作为API响应返回在一个文档中。
return new Document("version", response);
但是我得到的输出是:
{ "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" }] }
当我尝试在Mongo shell中运行相同的操作时,我得到了以下输出(这是正确的):
{
version:[
{
"version" : "1.10.1"
},
{
"version" : "1.10.0"
}
]
}
我的Java代码有什么问题?是版本还是代码错误?
非常感谢您的帮助。
英文:
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.
答案1
得分: 2
找到了问题
我调试了问题,发现排序在编码查询响应时使用了 normalization。默认情况下,该值为 false。因此,Shell 查询返回了正确的输出。
但在 Mongo-java-Driver 中,它将 normalization 设置为 true(默认设置)。
将构建器的 normalization 更新为 false,如下所示:
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);
这解决了我的问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论