querydrill

Learn ›MongoDB fundamentals ›Query operators

$in and $nin

find()$in$nin

$in is “field equals any of these”. $nin is its opposite, and it is less symmetric than it looks - a document missing the field entirely still matches $nin.

$in

db.users.find({
  status: {
    $in: ["active", "pending"]
  }
})

SQL-ish:

WHERE status IN ('active', 'pending')

$nin

db.users.find({
  status: {
    $nin: ["blocked", "deleted"]
  }
})

Be a little careful with $nin from a performance perspective, especially if the query matches a large portion of the collection.

Practise this

1 exercise