我想用 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/