如何在 Spring MongoDB 中将 2 个整数值连接成一个字符串
How to concatenate 2 integer values into a single string in Spring MongoDB
我在聚合阶段的文档如下所示:
{
month: 9,
year: 2017
}
现在我希望它变成:
{
time: "9/2017"
}
在 Mongo shell (v3.6.4) 中我可以做到这一点并且它按预期工作:
$concat: [{$substr:["$month", 0, -1]}, "/", {$substr:["$year", 0, -1]}]
在 4.0 版中有一个 $toString
运算符做同样的事情 $substr
.
但是 Spring MongoDB 中没有对应的代码,Stack Overflow 上的一些主题使用代码 .addExpression("concat(month,'/',year)")
。如果只有 month/year 字段是像 {month: "9"}
而不是 {month: 9}
.
这样的字符串,此代码片段才有效
我自己得到的:
project().andExpression("concat(substr(month,0,-1),'/',substr(year,0,-1))").as("time")
我在聚合阶段的文档如下所示:
{
month: 9,
year: 2017
}
现在我希望它变成:
{
time: "9/2017"
}
在 Mongo shell (v3.6.4) 中我可以做到这一点并且它按预期工作:
$concat: [{$substr:["$month", 0, -1]}, "/", {$substr:["$year", 0, -1]}]
在 4.0 版中有一个 $toString
运算符做同样的事情 $substr
.
但是 Spring MongoDB 中没有对应的代码,Stack Overflow 上的一些主题使用代码 .addExpression("concat(month,'/',year)")
。如果只有 month/year 字段是像 {month: "9"}
而不是 {month: 9}
.
我自己得到的:
project().andExpression("concat(substr(month,0,-1),'/',substr(year,0,-1))").as("time")