简体   繁体   English

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

[英]Spring Data Mongo - How to ask Mongo for the String ID

I have a projection and a groupby using Spring Data Mongo but at the moment when I call getMappedResults() I'm getting the BSON ID value rather than the String ID which I'd like.我有一个投影和一个 groupby 使用 Spring Data Mongo 但是在我调用getMappedResults()的那一刻,我得到的是 BSON ID 值而不是我想要的字符串 ID。

Is it possible to ask Mongo to return the ID as a string?是否可以要求 Mongo 将 ID 作为字符串返回? I know using raw queries I can call something like $toString: but how do I do this with my current codebase?我知道使用原始查询我可以调用$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");

You are almost there just need a small change in the group stage你已经差不多了,只需要在小组赛阶段做些小改动

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

This will return the string value.这将返回字符串值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM