在MongoDB中将一个或多个$ match组合在一起

梅西(Leo Messi):

有一种搜索对一列效果很好,我想让它检查两(更多)列中引入的字符串。

这是它的工作方式:

if (req.query.name) {
      aggregateOptions.push({ $match: { name: { $regex: req.query.name, $options: 'i' } } });
}

因此,它会检查名称列中是否存在引入的字符串/子字符串,并过滤掉不包含它的列。

我也想为城市添加此内容因此,它可以检查引入的字符串是否出现在名称城市列之一中,而不必同时出现在两个列中,也可以只出现在一个列中。

所以我$or为此:

if (req.query.name) {
  aggregateOptions.push({
    $or: [
      { $match: { name: { $regex: req.query.name, $options: 'i' } } },
      { $match: { city: { $regex: req.query.city, $options: 'i' } } },
    ],
  });
}

这似乎是错误的,因为在搜索框中引入字符串并且返回错误400和时,它不过滤任何内容"$or is not allowed in this atlas tier"

如何解决?

GitGitBoom:

你很亲密 但是$ or处于错误的级别。Atlas将可用阶段列入白名单。由于使用$ or作为阶段,因此没有出现该错误。

if (req.query.name) {
  aggregateOptions.push({
    $match: {
      $or: [
        { name: { $regex: req.query.name, $options: 'i' } },
        { city: { $regex: req.query.city, $options: 'i' } },
      ],
    }
  });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章