Spring Data Mongo-如何向Mongo询问字符串ID

问题描述 投票:1回答:1

我使用Spring Data Mongo进行投影和分组,但是当我调用getMappedResults()时,我得到的是BSON ID值,而不是我想要的字符串ID。

是否可以要求Mongo将ID作为字符串返回?我知道使用原始查询可以调用类似$toString:的代码,但是如何使用当前代码库执行此操作?

final ProjectionOperation dateProjection =
    project()
        .andInclude("_id", "name", "absolutePath")
        .and(dateField)
        .extractYear()
        .as("year");

final GroupOperation groupBy =
    group("year")
        .addToSet(
            new Document("id", "$_id") // How to get the String of the ID here
                .append("name", "$name")
                .append("absolutePath", "$absolutePath"))
        .as("results");
java mongodb spring-data-mongodb
1个回答
1
投票

您几乎在那里,只需要在小组赛中稍作改动

final GroupOperation groupBy =
        group("year")
            .addToSet(
                new Document("id", new Document("$toString","$_id"))
                    .append("name", "$name")
                    .append("absolutePath", "$absolutePath"))
            .as("results");

这将返回字符串值。

© www.soinside.com 2019 - 2024. All rights reserved.