Bird
Raised Fist0
MongoDBquery~20 mins

Boolean and null types in MongoDB - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Boolean and Null Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents where a field is explicitly false
Given a collection users with documents containing a subscribed field that can be true, false, or null, which query returns only users who have subscribed set to false (not null or missing)?
MongoDB
db.users.find({ subscribed: false })
Adb.users.find({ subscribed: { $ne: true } })
Bdb.users.find({ subscribed: false })
Cdb.users.find({ subscribed: { $exists: false } })
Ddb.users.find({ subscribed: null })
Attempts:
2 left
💡 Hint
Remember that false is different from null and missing fields.
🧠 Conceptual
intermediate
1:30remaining
Understanding null vs missing fields
In MongoDB, what is the difference between a field set to null and a field that is missing in a document?
AA field set to <code>null</code> exists with a null value; a missing field does not exist in the document.
BThere is no difference; both are treated the same in queries.
CA field set to <code>null</code> is ignored in queries, but missing fields cause errors.
DA missing field is stored as <code>null</code> internally by MongoDB.
Attempts:
2 left
💡 Hint
Think about whether the field is present in the document or not.
📝 Syntax
advanced
2:00remaining
Correctly query documents with boolean true or null
Which MongoDB query correctly finds documents where the active field is either true or null?
MongoDB
db.collection.find({ $or: [{ active: true }, { active: null }] })
Adb.collection.find({ active: { $in: [true, null] } })
Bdb.collection.find({ active: true || null })
Cdb.collection.find({ active: { $or: [true, null] } })
Ddb.collection.find({ $or: [{ active: true }, { active: null }] })
Attempts:
2 left
💡 Hint
Remember how to combine conditions with $or in MongoDB.
optimization
advanced
2:30remaining
Efficiently query documents with a boolean field false or missing
You want to find documents where the verified field is either false or does not exist. Which query is the most efficient and correct?
Adb.users.find({ $or: [{ verified: false }, { verified: { $exists: false } }] })
Bdb.users.find({ verified: { $ne: true } })
Cdb.users.find({ verified: false, verified: { $exists: false } })
Ddb.users.find({ verified: { $in: [false, null] } })
Attempts:
2 left
💡 Hint
Think about how to combine conditions for false and missing fields.
🔧 Debug
expert
2:00remaining
Identify the error in a query filtering null and false
What error or issue occurs with this MongoDB query?

db.orders.find({ status: false || null })
AThe query matches documents where status is false only, ignoring null.
BThe query returns all documents because false || null evaluates to false.
CThe query matches documents where status is null only, ignoring false.
DSyntaxError because MongoDB does not allow logical operators inside query values.
Attempts:
2 left
💡 Hint
Consider how JavaScript evaluates expressions before sending to MongoDB.

Practice

(1/5)
1. What does the Boolean type represent in MongoDB?
easy
A. Text strings
B. Numbers only
C. True or false values
D. Dates and times

Solution

  1. Step 1: Understand Boolean type meaning

    Boolean type stores only two possible values: true or false.
  2. Step 2: Compare with other data types

    Numbers, strings, and dates are different types, not Boolean.
  3. Final Answer:

    True or false values -> Option C
  4. Quick Check:

    Boolean = true/false [OK]
Hint: Boolean means true or false only [OK]
Common Mistakes:
  • Confusing Boolean with numbers
  • Thinking Boolean stores text
  • Mixing Boolean with date types
2. Which of the following is the correct way to store a null value in a MongoDB document?
easy
A. { "field": "null" }
B. { "field": false }
C. { "field": 0 }
D. { "field": null }

Solution

  1. Step 1: Identify null value syntax

    In MongoDB, null is stored as the keyword null without quotes.
  2. Step 2: Check other options

    "null" is a string, 0 is a number, false is Boolean, so they are incorrect.
  3. Final Answer:

    { "field": null } -> Option D
  4. Quick Check:

    Null stored as null keyword [OK]
Hint: Use null without quotes for null values [OK]
Common Mistakes:
  • Using "null" as a string instead of null
  • Confusing 0 or false with null
  • Putting null in quotes
3. Given the MongoDB collection documents:
{ "active": true }, { "active": false }, { "active": null }

What will the query db.collection.find({ "active": { $eq: null } }) return?
medium
A. Documents where active is true
B. Documents where active is null
C. Documents where active is false
D. No documents

Solution

  1. Step 1: Understand $eq: null behavior

    In MongoDB, querying with { field: { $eq: null } } matches documents where field exists and is exactly null. It does not match missing fields.
  2. Step 2: Apply to given documents

    Only the document with "active": null matches; documents with true or false do not.
  3. Final Answer:

    Documents where active is null -> Option B
  4. Quick Check:

    $eq: null matches existing null [OK]
Hint: $eq: null matches existing null fields [OK]
Common Mistakes:
  • Thinking $eq: null matches missing fields
  • Confusing false or true as null
  • Expecting no results
4. You wrote this MongoDB query to find documents where isActive is false:
db.users.find({ isActive: False })

But it returns an error. What is the problem?
medium
A. Boolean false must be lowercase: false
B. Field name should be in quotes
C. Use $eq operator for Boolean
D. MongoDB does not support Boolean queries

Solution

  1. Step 1: Check Boolean syntax in MongoDB queries

    Boolean values in MongoDB queries must be lowercase: true or false.
  2. Step 2: Identify error cause

    Using capitalized False causes a syntax error because it's not recognized.
  3. Final Answer:

    Boolean false must be lowercase: false -> Option A
  4. Quick Check:

    Boolean literals are lowercase in queries [OK]
Hint: Use lowercase true/false in queries [OK]
Common Mistakes:
  • Using capitalized True or False
  • Omitting quotes on field names (allowed but not error)
  • Thinking $eq is required for Boolean
5. You want to find documents where the field verified is either false or null. Which MongoDB query correctly finds these documents?
hard
A. { verified: { $in: [false, null] } }
B. { verified: false }
C. { verified: { $ne: true } }
D. { verified: { $exists: false } }

Solution

  1. Step 1: Understand the requirement

    We want documents where verified is false OR null.
  2. Step 2: Analyze each option

    { verified: false } matches only false, not null. { verified: { $ne: true } } matches existing fields != true (false, null, other values), but not missing fields and includes unwanted values. { verified: { $exists: false } } matches only missing, not false. { verified: { $in: [false, null] } } uses $in to match false or null exactly.
  3. Final Answer:

    { verified: { $in: [false, null] } } -> Option A
  4. Quick Check:

    $in matches multiple values including null [OK]
Hint: Use $in with false and null to match both [OK]
Common Mistakes:
  • Using only false without null
  • Using $exists which misses false
  • Using $ne: true which matches unwanted values