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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.