MongoDb $filter 与 $or 未按预期工作

问题描述 投票:0回答:1

请打开这个游乐场,跟我来:游乐场

我有一个名为 Pages 的集合,其中每个文档都有键 Urls 和 DraftUrls,它是一个对象数组,如下所示:

IncomingUrl: string;
EclipseId: string;
Status: string;
RedirectionLink: string;
Validity: Date;
Config: ConfigData[];
Indexing: boolean;

现在,我只想查找状态为“非活动”或“有效期”已过期的 URL 的数量。我还想要更多信息,例如 GroupName 和 TemplateName。

我尝试将 $filter$or 一起使用,正如您在操场上看到的那样,但问题是它给了我所有 URL 的计数,而不检查 cond 中的指定条件。

起初我以为检查有效性的方式存在问题,但后来我删除了该部分并仅检查非活动状态,但它仍然给了我所有 URL 的计数

javascript mongodb
1个回答
0
投票

您的

cond
中的
$filter
表达式不正确。修改
$filter
运算符应如下所示:

{
    $addFields: {
      filteredUrls: {
        $filter: {
          input: isDraftB ? '$DraftUrls' : '$Urls',
          cond: {
            $or: [
              {
                $eq: [
                  "$$this.Status",
                  "Inactive"
                ]
              },
              {
                $and: [
                  {
                    $eq: [
                      "$$this.Status",
                      "Validity"
                    ]
                  },
                  {
                    $gte: [
                      "$$this.Validity",
                      currentDate
                    ]
                  }
                ]
              }
            ]
          }
        }
      }
    }
  },
© www.soinside.com 2019 - 2024. All rights reserved.