MongoServerError:无效的 $set :: 由 :: 表达式 $in 导致,恰好需要 2 个参数。 1 已通过

问题描述 投票:0回答:1
{
    "offers" : [
        {
            "name" : "min",
            "prices" : [
               8, 15, 18
            ]
        },
        {
            "name" : "max",
            "prices" : [
               9, 11, 18
            ]
        }
    ],
    "nl" : [
        2, 4, 6, 7
    ]
}

更新聚合不起作用。两种方法我都试过了

[
    {
      $set: {
          offers: {
              $cond: {
                  if: {
                        "nl": {$in: [7]}
                  },
                   then: {
                          $concatArrays: ["$offers", []]
                      },
                  else: {
                        $concatArrays: ["$offers", [{
                                "name" : "minimal",
                                "prices" : []
                            }]]
                      }
              }
          }
      }
    },
   ]

下面的代码不起作用,也没有显示任何错误。

[
    {
      $set: {
          offers: {
              $cond: {
                  if: {
                        $in: ["$nl", 7]
                  },
                   then: {
                          $concatArrays: ["$offers", []]
                      },
                  else: {
                        $concatArrays: ["$offers", [{
                                "name" : "minimal",
                                "prices" : []
                            }]]
                      }
              }
          }
      }
    },
   ]

我想根据“nl”字段中 7 的 exi/stence 来取消 Offers 数组。请帮我。我正在尝试在 5 小时内解决这个问题。

mongodb mongoose mongodb-query
1个回答
0
投票

$in 聚合采用以下语法:

{ $in: [ <expression>, <array expression> ] }

您需要反转 $in 条件。像这样:

$in: [7, "$nl"]
© www.soinside.com 2019 - 2024. All rights reserved.