Script Valley
MongoDB: Complete Course
CRUD OperationsLesson 2.5

MongoDB query operators complete reference: $in, $exists, $regex and more

$in, $nin, $exists, $type, $regex, $elemMatch, $size, array query operators, dot notation queries, nested document queries

Field-level and array operators

MongoDB query operators

Beyond comparison operators, MongoDB provides operators to filter by value membership, field presence, data type, regex pattern, and array element conditions. These are the building blocks of any non-trivial query.

// $in โ€” match any value from an explicit list
db.collection('products').find({
  category: { $in: ['laptop', 'tablet', 'phone'] }
})

// $nin โ€” exclude any value in the list
db.collection('products').find({
  status: { $nin: ['discontinued', 'draft'] }
})

// $exists โ€” document has the field (true) or lacks it (false)
db.collection('users').find({ email: { $exists: true } })

// $type โ€” match a specific BSON type
db.collection('data').find({ value: { $type: 'string' } })

// $regex โ€” pattern match on string fields
db.collection('users').find({ name: { $regex: /^Ada/i } })

// $size โ€” array has exactly this many elements
db.collection('posts').find({ tags: { $size: 3 } })

// $elemMatch โ€” one array element must satisfy ALL conditions
db.collection('orders').find({
  items: { $elemMatch: { sku: 'A1', qty: { $gt: 2 } } }
})

Dot notation for nested and array fields

// Query inside an embedded document
db.collection('users').find({ 'address.city': 'London' })

// Query a specific array index position
db.collection('scores').find({ 'grades.0': { $gte: 90 } })

Always quote dot-notation keys as strings in JavaScript. An unquoted key containing a dot is a nested object property access, not a MongoDB path, and will silently produce wrong results or a syntax error.

MongoDB query operators complete reference: $in, $exists, $regex and more โ€” CRUD Operations โ€” MongoDB: Complete Course โ€” Script Valley โ€” Script Valley