2

how to query an array of values using the $ne query in mongodb?

this is the list i would like to query

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,
  },
]

this below is the query i tried, i would like to get back everything not equal "crystal","marry" but instead this query below is returning everything

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

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

1 Answer 1

2

You're close, but in this case you should use the $nin (not in array) operator rather than $ne (not equal). Like so:

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

You can check it out here.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.