簡體   English   中英

如何將 $concat 與 $cond 與 MongoDB Java 驅動程序一起使用

[英]How to use $concat with a $cond with MongoDB Java drivers

我想在 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"));

但它不起作用,我收到錯誤消息“'Unrecognized expression '$$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")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM