【问题标题】:how to query an array of values using the $ne query in mongodb?如何使用 mongodb 中的 $ne 查询来查询值数组?
【发布时间】:2020-02-24 21:22:40
【问题描述】:

如何在 mongodb 中使用 $ne 查询来查询一组值?

这是我要查询的列表

const movies = [
  {
    name: 'crystal',
    showWatched: 'cars',
    number: 1,
  },
  {
    name: 'barbra',
    showWatched: 'moonlight',
    number: 2,
  },
  {
    name: 'marry',
    showWatched: 'sunshine',
    number: 3,
  },
 {
    name: 'joy',
    showWatched: 'cooking',
    number: 4,
  },
]

以下是我尝试过的查询,我想取回不等于 "crystal","marry" 的所有内容,但下面的此查询正在返回所有内容

const findin = ["crystal","marry"]

db.getCollection('movies').find({name: {$ne: findin} })

【问题讨论】:

    标签: arrays node.js mongodb mongoose


    【解决方案1】:

    您已经接近了,但在这种情况下,您应该使用$nin(不在数组中)运算符而不是$ne(不等于)。像这样:

    db.getCollection('movies').find({name: {$nin: findin} })
    

    您可以查看here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2019-12-27
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多