Boolean and null types in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to work with Boolean and null types in MongoDB changes as we handle more data.
Specifically, we ask: how does checking or filtering these types grow when the data grows?
Analyze the time complexity of the following code snippet.
db.collection.find({
isActive: true,
deletedAt: null
})
.limit(10)
This code finds up to 10 documents where the field isActive is true and deletedAt is null.
Look for repeated work done by the database when running this query.
- Primary operation: Scanning documents to check if
isActiveis true anddeletedAtis null. - How many times: Once for each document until 10 matches are found or all documents are checked.
As the number of documents grows, the database checks more documents to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows roughly in direct proportion to the number of documents until the limit is reached.
Time Complexity: O(n)
This means the time to find matching documents grows linearly with the number of documents in the collection.
[X] Wrong: "Checking for Boolean or null fields is instant no matter how many documents there are."
[OK] Correct: The database must look at each document to check these fields unless there is an index, so more documents mean more work.
Understanding how simple filters on Boolean and null fields scale helps you explain query performance clearly and confidently in real-world situations.
What if we added an index on isActive? How would the time complexity change?
Practice
Boolean type represent in MongoDB?Solution
Step 1: Understand Boolean type meaning
Boolean type stores only two possible values: true or false.Step 2: Compare with other data types
Numbers, strings, and dates are different types, not Boolean.Final Answer:
True or false values -> Option CQuick Check:
Boolean = true/false [OK]
- Confusing Boolean with numbers
- Thinking Boolean stores text
- Mixing Boolean with date types
Solution
Step 1: Identify null value syntax
In MongoDB, null is stored as the keywordnullwithout quotes.Step 2: Check other options
"null" is a string, 0 is a number, false is Boolean, so they are incorrect.Final Answer:
{ "field": null } -> Option DQuick Check:
Null stored as null keyword [OK]
- Using "null" as a string instead of null
- Confusing 0 or false with null
- Putting null in quotes
{ "active": true }, { "active": false }, { "active": null }What will the query
db.collection.find({ "active": { $eq: null } }) return?Solution
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.Step 2: Apply to given documents
Only the document with "active": null matches; documents with true or false do not.Final Answer:
Documents where active is null -> Option BQuick Check:
$eq: null matches existing null [OK]
- Thinking $eq: null matches missing fields
- Confusing false or true as null
- Expecting no results
isActive is false:db.users.find({ isActive: False })But it returns an error. What is the problem?
Solution
Step 1: Check Boolean syntax in MongoDB queries
Boolean values in MongoDB queries must be lowercase: true or false.Step 2: Identify error cause
Using capitalized False causes a syntax error because it's not recognized.Final Answer:
Boolean false must be lowercase: false -> Option AQuick Check:
Boolean literals are lowercase in queries [OK]
- Using capitalized True or False
- Omitting quotes on field names (allowed but not error)
- Thinking $eq is required for Boolean
verified is either false or null. Which MongoDB query correctly finds these documents?Solution
Step 1: Understand the requirement
We want documents where verified is false OR null.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.Final Answer:
{ verified: { $in: [false, null] } } -> Option AQuick Check:
$in matches multiple values including null [OK]
- Using only false without null
- Using $exists which misses false
- Using $ne: true which matches unwanted values
