Spring 数据 Mongo - 如何向 Mongo 询问字符串 ID
Spring Data Mongo - How to ask Mongo for the String ID
我使用 Spring 数据 Mongo 有一个投影和一个 groupby 但在我调用 getMappedResults()
的那一刻,我得到的是 BSON ID 值而不是字符串 ID我想要。
是否可以向 Mongo 询问 return 字符串形式的 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");
你已经差不多了,只需要在小组赛阶段做一些小改动
final GroupOperation groupBy =
group("year")
.addToSet(
new Document("id", new Document("$toString","$_id"))
.append("name", "$name")
.append("absolutePath", "$absolutePath"))
.as("results");
这将 return 字符串值。
我使用 Spring 数据 Mongo 有一个投影和一个 groupby 但在我调用 getMappedResults()
的那一刻,我得到的是 BSON ID 值而不是字符串 ID我想要。
是否可以向 Mongo 询问 return 字符串形式的 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");
你已经差不多了,只需要在小组赛阶段做一些小改动
final GroupOperation groupBy =
group("year")
.addToSet(
new Document("id", new Document("$toString","$_id"))
.append("name", "$name")
.append("absolutePath", "$absolutePath"))
.as("results");
这将 return 字符串值。