Loading

Quipoin Menu

Learn • Practice • Grow

mongodb / Query Operators
tutorial

Query Operators

Now that you know how to find documents with exact matches, it's time to level up! MongoDB provides powerful query operators that let you find documents based on conditions like greater than, less than, or matching values in arrays.

What are Query Operators?

Query operators are special keys that start with $ and allow you to specify conditions in your queries. They make your searches much more powerful and flexible.

Think of query operators as special instructions like "find me products cheaper than $50" or "find users who are either 25 or 30 years old".

Comparison Operators

OperatorMeaningExample
$gtGreater than{ price: { $gt: 100 } }
$gteGreater than or equal{ age: { $gte: 18 } }
$ltLess than{ stock: { $lt: 10 } }
$lteLess than or equal{ rating: { $lte: 3 } }
$eqEqual to{ status: { $eq: "active" } }
$neNot equal{ status: { $ne: "deleted" } }
$inIn array{ age: { $in: [25, 30, 35] } }
$ninNot in array{ category: { $nin: ["books", "toys"] } }

Examples with Comparison Operators
<!-- Find products cheaper than $50 -->
db.products.find({ price: { $lt: 50 } }).pretty()

<!-- Find users aged 25 or 30 -->
db.users.find({ age: { $in: [25, 30] } }).pretty()

<!-- Find expensive items (price >= 500) -->
db.products.find({ price: { $gte: 500 } }).pretty()

Logical Operators

Use $and, $or, $not, and $nor to combine multiple conditions.

OperatorDescription
$andMatches if all conditions are true
$orMatches if at least one condition is true

Example with $or:
<!-- Find products that are either electronics OR cheaper than $20 -->
db.products.find({
  $or: [
    { category: "electronics" },
    { price: { $lt: 20 } }
  ]
}).pretty()

Example with $and (implied):
<!-- Electronics cheaper than $100 (implied AND) -->
db.products.find({
  category: "electronics",
  price: { $lt: 100 }
})

<!-- Explicit AND (same result) -->
db.products.find({
  $and: [
    { category: "electronics" },
    { price: { $lt: 100 } }
  ]
})

Element Operators

OperatorDescription
$existsMatches documents that have the specified field
$typeMatches if field is of specified type

<!-- Find documents with an email field -->
db.users.find({ email: { $exists: true } })

<!-- Find documents where age is a number -->
db.users.find({ age: { $type: "number" } })

Array Query Operators

OperatorDescription
$sizeMatches arrays with specified number of elements
$allMatches arrays that contain all specified elements

<!-- Find posts with exactly 2 tags -->
db.posts.find({ tags: { $size: 2 } })

<!-- Find posts tagged with both 'mongodb' AND 'nodejs' -->
db.posts.find({ tags: { $all: ["mongodb", "nodejs"] } })

Two Minute Drill

  • Query operators start with $ and add conditions to your queries.
  • Comparison operators: $gt, $lt, $gte, $lte, $in, $nin.
  • Logical operators: $and, $or for combining conditions.
  • Element operators: $exists, $type for checking field existence and type.
  • Array operators: $size, $all for querying arrays.
  • These operators make MongoDB queries incredibly powerful and flexible.

Need more clarification?

Drop us an email at career@quipoinfotech.com