Stop 05 · MongoDB FundamentalsTopics · 6/9

Query Operators

Part of the MERN Stack Roadmap

In short

"Products under Rs 500 in books or stationery" is a typical filter. Query operators like $lt and $in let you express it exactly, and the same filter object works in mongosh, the Node driver and Mongoose.

Operators like $eq, $gt, $lt, $in, $and, $or and $regex let you write precise filters, such as products under 500 rupees in the books or stationery category.

The jaadu adjusts a row of big chunky sliders and switches on a control panel while a conveyor belt sorts boxes into different bins.

Think of it like this

Filters on a shopping app

On a shopping app you tick categories, drag a price slider and type a brand name. Each control is a rule. MongoDB operators are those controls written as JSON: $in for the ticked boxes, $lt for the slider and $regex for the typed text.

Watch it happen

{ price: { $lt: 500 }, category: { $in: ["books", "stationery"] } }

Novel 350 books
0
Pen 40 stationery
1
Lamp 900 home
2
Atlas 650 books
3
Mug 250 kitchen
4

1/4Five products in the collection.

The big ideas

What you need to know

Comparison

$eq, $ne, $gt, $gte, $lt, $lte. { price: 500 } is shorthand for { price: { $eq: 500 } }.

$in and $nin

Match any (or none) of a list of values. Cleaner than many $or clauses on the same field.

$and and $or

Fields in one object are already ANDed. Use $or for alternatives, like cheap OR on sale.

$exists and $regex

$exists checks if a field is present. $regex matches patterns but can be slow without an anchored index.

See it in code

Walk through the code

js
1// Under Rs 500 in books or stationery2db.products.find({3  price: { $lt: 500 },4  category: { $in: ["books", "stationery"] },5});6 7// Either on sale OR rated 4.5 and above8db.products.find({9  $or: [{ onSale: true }, { rating: { $gte: 4.5 } }],10});11 12// Price between 200 and 800, has a discount field13db.products.find({14  price: { $gte: 200, $lte: 800 },15  discount: { $exists: true },16});17 18// Name starts with "Steel", any case19db.products.find({ name: { $regex: "^steel", $options: "i" } });

1/4Two fields in one object: both must match.

Four common filters. Copy them into your Express code later with almost no change.

Save yourself the pain

Common mistakes

Oops

Writing { price: { $gt: 200 }, price: { $lt: 800 } } for a range.

Do this instead

Duplicate keys overwrite each other. Put both operators in one object: { price: { $gt: 200, $lt: 800 } }.

Oops

Using $or for several values of the same field.

Do this instead

Use $in: { category: { $in: ["books", "toys"] } } is shorter and faster.

Your mission

Answer five shop questions

~25 min

Quick check

Which filter finds products that are NOT in the toys category?

Go deeper

  1. 1Find products with price between 200 and 1000 rupees using $gte and $lte.
  2. 2Use $in to match any of several cities, such as Pune, Indore or Kochi.
  3. 3Query inside nested fields and arrays with dot notation.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge