Question 10 / 50
Must KnowMediumDebuggingQuerying
This query returns nothing, even though the user exists. What is wrong, and how would you fix it?
db.users.findOne({
_id: "64a1b2c3d4e5f6a7b8c90001"
})Think about it first.
Short answer
_id is stored as an ObjectId, not a string. A string equality never matches, so the query returns null. Cast the value with ObjectId before querying — or let the driver/Mongoose cast it.
Why?
BSON types are part of equality. The same trap shows up with dates stored as Date vs ISO strings, and with numbers stored as int vs string. Always check the stored type, not just the value you see in a GUI that stringifies ObjectIds.
Example
db.users.findOne({
_id: ObjectId("64a1b2c3d4e5f6a7b8c90001")
})Interview tip
Say 'I would check the BSON type in Compass or with $type, then cast in the driver.' That is a production debugging instinct.
Common mistake
Assuming Compass copying the id into a string filter is the same as the stored type.
How did you do?
Cheat Sheet