0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
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.