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

时间:2019-10-29 15:40:35

标签: arrays node.js mongodb mongoose

如何在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} })

1 个答案:

答案 0 :(得分:2)

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

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

您可以签出here.

相关问题