Bird
Raised Fist0
MongoDBquery~10 mins

Query filter syntax in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Query filter syntax
Start with collection
Apply filter object
Match documents
Return matched documents
End
The query filter syntax in MongoDB starts by applying a filter object to a collection, matching documents that meet the filter criteria, and returning those matched documents.
Execution Sample
MongoDB
db.users.find({ age: { $gt: 25 } })
This query finds all documents in the 'users' collection where the 'age' field is greater than 25.
Execution Table
StepFilter AppliedDocument CheckedMatch ResultAction
1{ age: { $gt: 25 } }{ name: 'Alice', age: 22 }NoSkip document
2{ age: { $gt: 25 } }{ name: 'Bob', age: 30 }YesInclude document
3{ age: { $gt: 25 } }{ name: 'Carol', age: 25 }NoSkip document
4{ age: { $gt: 25 } }{ name: 'Dave', age: 40 }YesInclude document
5No more documents--Return matched documents
💡 All documents checked; query returns documents where age > 25
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Matched Documents[][][{ name: 'Bob', age: 30 }][{ name: 'Bob', age: 30 }][{ name: 'Bob', age: 30 }, { name: 'Dave', age: 40 }][{ name: 'Bob', age: 30 }, { name: 'Dave', age: 40 }]
Key Moments - 2 Insights
Why does the document with age 25 not match the filter { age: { $gt: 25 } }?
Because $gt means 'greater than', so age must be strictly more than 25. The document with age 25 is equal, not greater, so it does not match (see execution_table row 3).
What happens if the filter is an empty object {}?
An empty filter matches all documents in the collection, so all documents would be returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which document is included at step 2?
A{ name: 'Alice', age: 22 }
B{ name: 'Carol', age: 25 }
C{ name: 'Bob', age: 30 }
D{ name: 'Dave', age: 40 }
💡 Hint
Check the 'Document Checked' and 'Match Result' columns at step 2 in the execution_table.
At which step does the query finish checking documents?
AStep 4
BStep 5
CStep 3
DStep 2
💡 Hint
Look for the row where 'No more documents' is noted in the 'Filter Applied' column.
If the filter changed to { age: { $gte: 25 } }, which document would now match that did not before?
A{ name: 'Carol', age: 25 }
B{ name: 'Bob', age: 30 }
C{ name: 'Alice', age: 22 }
D{ name: 'Dave', age: 40 }
💡 Hint
Compare the difference between $gt and $gte operators and check the document ages in variable_tracker.
Concept Snapshot
MongoDB query filter syntax:
- Use an object to specify conditions, e.g., { field: value }
- Use operators like $gt (greater than), $gte (greater or equal)
- Filters match documents that satisfy all conditions
- Result is all matched documents from the collection
- Empty filter {} returns all documents
Full Transcript
This visual execution shows how MongoDB applies a query filter to a collection. The filter { age: { $gt: 25 } } is applied to each document. Documents with age greater than 25 are matched and included in the result. Documents with age 25 or less are skipped. The process continues until all documents are checked, then the matched documents are returned. Key points include understanding the $gt operator means strictly greater than, and that an empty filter matches all documents.

Practice

(1/5)
1. What does a MongoDB query filter do?
{ age: 25 }
easy
A. Deletes documents where age is 25
B. Updates documents to set age to 25
C. Selects documents where the age field is exactly 25
D. Creates a new document with age 25

Solution

  1. Step 1: Understand the role of a query filter

    A query filter in MongoDB is used to find documents that match certain conditions, not to modify or delete them.
  2. Step 2: Analyze the filter { age: 25 }

    This filter matches documents where the field age has the value 25 exactly.
  3. Final Answer:

    Selects documents where the age field is exactly 25 -> Option C
  4. Quick Check:

    Query filter = select matching documents [OK]
Hint: Filters pick matching data, not modify or delete [OK]
Common Mistakes:
  • Confusing filter with update or delete commands
  • Thinking filter creates or changes documents
  • Assuming filter matches partial or range values without operators
2. Which of these is the correct syntax to find documents where score is greater than 80?
easy
A. { score > 80 }
B. { score: gt 80 }
C. { $score: { gt: 80 } }
D. { score: { $gt: 80 } }

Solution

  1. Step 1: Recall MongoDB operator syntax

    MongoDB uses JSON objects with operators starting with a dollar sign, like $gt for 'greater than'.
  2. Step 2: Check each option

    { score: { $gt: 80 } } uses { score: { $gt: 80 } } which is correct syntax. Others miss the dollar sign or use invalid syntax.
  3. Final Answer:

    { score: { $gt: 80 } } -> Option D
  4. Quick Check:

    Use $gt for greater than in filters [OK]
Hint: Operators start with $ inside field object [OK]
Common Mistakes:
  • Omitting $ before operator
  • Using comparison operators like > directly
  • Wrong operator names without $
3. Given the collection documents:
{ name: "Alice", age: 30 }
{ name: "Bob", age: 25 }
{ name: "Carol", age: 30 }

What will this query return?
db.collection.find({ age: 30 })
medium
A. [{ name: "Alice", age: 30 }, { name: "Carol", age: 30 }]
B. [{ name: "Bob", age: 25 }]
C. [] (empty array)
D. Syntax error

Solution

  1. Step 1: Understand the filter condition

    The filter { age: 30 } matches documents where the age field equals 30.
  2. Step 2: Identify matching documents

    Documents for Alice and Carol have age 30, so both match. Bob has age 25, so does not match.
  3. Final Answer:

    [{ name: "Alice", age: 30 }, { name: "Carol", age: 30 }] -> Option A
  4. Quick Check:

    Filter matches exact age 30 documents [OK]
Hint: Filter matches exact field values without operators [OK]
Common Mistakes:
  • Expecting only one document returned
  • Confusing filter with update or delete
  • Thinking filter matches partial or range without operators
4. What is wrong with this MongoDB query filter?
db.collection.find({ age: $gt: 20 })
medium
A. Using $gt instead of $gte
B. Missing curly braces around $gt operator
C. Field name should be $age, not age
D. No error, query is correct

Solution

  1. Step 1: Check operator syntax in filter

    MongoDB requires operators like $gt to be inside an object as a value for the field key.
  2. Step 2: Identify the syntax error

    The query uses { age: $gt: 20 } which is invalid. It should be { age: { $gt: 20 } } with curly braces around $gt: 20.
  3. Final Answer:

    Missing curly braces around $gt operator -> Option B
  4. Quick Check:

    Operators must be inside braces as field values [OK]
Hint: Put operators inside braces after field name [OK]
Common Mistakes:
  • Omitting braces around operator
  • Using wrong operator names
  • Misplacing $ sign
5. You want to find documents where status is either "active" or "pending". Which filter correctly does this?
hard
A. { status: { $in: ["active", "pending"] } }
B. { status: { $or: ["active", "pending"] } }
C. { $or: { status: "active", status: "pending" } }
D. { status: "active" || "pending" }

Solution

  1. Step 1: Understand how to match multiple values

    MongoDB uses $in operator to match a field against any value in a list.
  2. Step 2: Analyze each option

    { status: { $in: ["active", "pending"] } } uses { status: { $in: ["active", "pending"] } } which is correct. { status: { $or: ["active", "pending"] } } uses invalid operator $or inside field. { $or: { status: "active", status: "pending" } } has wrong syntax for $or. { status: "active" || "pending" } uses invalid JavaScript syntax.
  3. Final Answer:

    { status: { $in: ["active", "pending"] } } -> Option A
  4. Quick Check:

    Use $in for matching any value in list [OK]
Hint: Use $in with array for multiple value matches [OK]
Common Mistakes:
  • Using $or inside field instead of top-level
  • Wrong syntax for $or operator
  • Using JavaScript operators inside filter