作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 Java 做这样的事情:
$concat: [
"$$value",
{
$cond: {
if: { $eq: [ "$$value", "My hobbies include:" ] },
then: " ",
else: ", "
}
},
"$$this"
]
来源:https://docs.mongodb.com/manual/reference/operator/aggregation/reduce/#string-concatenation
所以我的版本是:
Document concat = new Document("$concat", Arrays.asList(
"$$value",
new Document("$cond", new Document()
.append("if", eq("$$value", ""))
.append("then", "")
.append("else", " - ")),
"$$this.category"));
但它不起作用,我收到错误消息“'无法识别的表达式'$$value'”
有什么想法吗?谢谢!
最佳答案
所以我找到了一个解决方案,但这并不完全是我想要的(我必须使用 BsonDocument):
BsonArray eq = new BsonArray();
eq.add(new BsonString("$$value"));
eq.add(new BsonString(""));
Document concat = new Document("$concat", Arrays.asList(
new BsonString("$$value"),
new BsonDocument("$cond", new BsonDocument()
.append("if", new BsonDocument("$eq", eq))
.append("then", new BsonString(""))
.append("else", new BsonString(" - "))),
new BsonString("$$this.category")));
关于java - 如何将 $concat 与 $cond 与 MongoDB Java 驱动程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62023840/
我是一名优秀的程序员,十分优秀!