从MongoDB的排序中获得了不想要的输出

huangapple go评论75阅读模式
英文:

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

&lt;dependency&gt;
	&lt;groupId&gt;org.mongodb&lt;/groupId&gt;
	&lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
	&lt;version&gt;3.8.0&lt;/version&gt;
&lt;/dependency&gt;

I have created the index with collation:

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

I have implemented the aggregation query with java driver:

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

ArrayList&lt;Document&gt; response = new ArrayList&lt;&gt;();

ArrayList&lt;Bson&gt; aggregate = new ArrayList&lt;Bson&gt;(Arrays.asList(
  match(gt(&quot;version&quot;, &quot;1.9.4&quot;)), sort(descending(&quot;version&quot;)),
  project(fields(include(&quot;version&quot;), exclude(&quot;_id&quot;)))
));

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(&quot;version&quot;, response);

But the output I'm getting is:

{ &quot;version&quot; : [{ &quot;version&quot; : &quot;\u000f\u0003\b\u000f\f\b\u000f\u0003\u0001\t\u0001\t&quot; }, { &quot;version&quot; : &quot;\u000f\u0003\b\u000f\f\b\u000f\u0002\u0001\t\u0001\t&quot; }] }

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

{
  version:[
    {
    &quot;version&quot; : &quot;1.10.1&quot;
    },
    {
    &quot;version&quot; : &quot;1.10.0&quot;
    }
  ]
}

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(&quot;en&quot;).numericOrdering(true).normalization(false).build();
ArrayList&lt;Document&gt; response = new ArrayList&lt;&gt;();

ArrayList&lt;Bson&gt; aggregate = new ArrayList&lt;Bson&gt;(Arrays.asList(
  match(gt(&quot;version&quot;, &quot;1.9.4&quot;)), sort(descending(&quot;version&quot;)),
  project(fields(include(&quot;version&quot;), exclude(&quot;_id&quot;)))
));

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

This fixed my issue.

huangapple
  • 本文由 发表于 2020年5月4日 20:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61591841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定