【问题标题】:Mongodb: How to find all docs that a specific text is not in their specific field?Mongodb:如何找到特定文本不在其特定领域的所有文档?
【发布时间】:2023-02-14 11:25:26
【问题描述】:

假设我们有这个文档数组:

[ {name: 'john'},{name: 'joy'},{name: 'martin'},{name: 'tom'},{name: 'eli'}]

我们想找到所有name 字段不包含 'm' 字符的文档。 我尝试在 Express.js 应用程序中使用 Mongoose:

Collection.find({name:{$regex:{$not:'m'}}})

显然没有用。

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    您正在寻找反转正则表达式匹配结果,而不是字符集,所以将 $not 放在 $regex 之外:

    Collection.find({name:{$not:{$regex:'m'}}})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      相关资源
      最近更新 更多