Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

MDB
MongoDB
0/10 completed
Beginner
What is MongoDB?Inserting and finding documentsUpdating and deleting
Intermediate
Query operatorsSorting, paging and projectionIndexes and explain()
Advanced
The aggregation pipelineJoins, facets and window functionsSchema design and validation
Pro
Drivers, transactions and scaling
Tracks / MongoDB / Intermediate

Query operators

Equality filters like { category: 'Kitchen' } only go so far. Query operators, which all start with $, let you ask for ranges, lists, missing fields, patterns and conditions on arrays.

Comparison

Operator Meaning
$eq / $ne equal / not equal
$gt / $gte greater than / or equal
$lt / $lte less than / or equal
$in / $nin in a list / not in a list
MongoDB shell (simulated) · editable

Several operators on one field combine with AND: { price: { $gte: 40, $lte: 100 } } means 40 ≤ price ≤ 100.

Dates compare like numbers:

MongoDB shell (simulated) · editable

Logical operators

Fields at the top level are already ANDed. Use $or when any condition may match:

MongoDB shell (simulated) · editable
Operator Meaning
$and All conditions (needed only when the same field appears twice, or to combine several $ors)
$or At least one condition
$nor None of the conditions
$not Inverts an operator: { price: { $not: { $gt: 50 } } }
MongoDB shell (simulated) · editable

Field existence and type

Documents can differ, so you often ask "does this field exist?":

MongoDB shell (simulated) · editable

Watch out for null: { field: null } matches documents where the field is null or missing:

MongoDB shell (simulated) · editable

$type checks the BSON type, which is handy for finding messy data such as prices stored as strings: { price: { $type: 'string' } }.

Regular expressions

MongoDB shell (simulated) · editable

A regex anchored at the start (/^abc/, case-sensitive) can use an index. Others scan every document, so for real search use Atlas Search or a text index.

Querying arrays

Query Matches when…
{ tags: 'work' } any element equals 'work'
{ tags: { $all: ['work', 'computer'] } } the array contains all of these
{ tags: { $size: 2 } } the array has exactly 2 elements
{ tags: ['work', 'gift'] } the array is exactly this, in this order
{ 'tags.0': 'work' } the first element is 'work'
MongoDB shell (simulated) · editable

Arrays of objects and $elemMatch

Orders contain an items array of objects. Dot notation reaches into each element:

MongoDB shell (simulated) · editable

But be careful when you combine two conditions on array elements. This query asks for orders with an item of qty >= 5 and an item priced over 5, and they can be different items:

MongoDB shell (simulated) · editable

The Notebook order matched because one item has qty 10 (a $3.50 notebook) and another item costs $6.75. To require one single element to match all conditions, use $elemMatch:

MongoDB shell (simulated) · editable

Expressions: comparing two fields

$expr lets you use aggregation expressions in a query, for example to compare two fields of the same document:

MongoDB shell (simulated) · editable

'$stock' with a dollar sign means "the value of the stock field".

Practice

  1. Find products that cost less than 10 or have the tag 'audio'.
  2. Find customers aged 25 to 35 (inclusive) who are not in India.
  3. Find orders containing at least one item with qty of 4 or more, using $elemMatch.
  4. Find customers whose email doesn't end in example.com. (Expect none!)
MongoDB shell (simulated) · editable
✎ Check your understanding

1.Which filter finds products priced from 10 to 50 inclusive?

2.What does { nickname: null } match?

3.You need orders where a single item has both qty ≥ 5 and price > 5. What do you use?

← Updating and deleting