0
0
MongoDBquery~20 mins

findOne method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB findOne Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find the first user named 'Alice'
Given a collection users with documents containing name and age, what will this query return?

db.users.findOne({ name: 'Alice' })
MongoDB
db.users.findOne({ name: 'Alice' })
AAll documents where the name is 'Alice'
BThe first document where the name is 'Alice', including all fields
CAn array of documents with name 'Alice'
DAn error because findOne requires a projection
Attempts:
2 left
💡 Hint
Remember, findOne returns only one document, not multiple.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this findOne query
Which option shows the correct syntax for finding a user with age 30 using findOne?
MongoDB
db.users.findOne({ age: 30 })
Adb.users.findOne({ age: 30 })
Bdb.users.findOne(age: 30)
Cdb.users.findOne({age == 30})
Ddb.users.findOne({ 'age': '30' })
Attempts:
2 left
💡 Hint
The query filter must be an object with key-value pairs.
query_result
advanced
2:00remaining
What does this findOne query return with projection?
Given documents with fields name, age, and city, what will this query return?

db.users.findOne({ name: 'Bob' }, { age: 1, _id: 0 })
MongoDB
db.users.findOne({ name: 'Bob' }, { age: 1, _id: 0 })
AAn error because projection keys must be strings
BA document with all fields of the first user named 'Bob'
CA document with age and _id fields of the first user named 'Bob'
DA document with only the age field of the first user named 'Bob', excluding _id
Attempts:
2 left
💡 Hint
Projection controls which fields are included or excluded in the result.
🔧 Debug
advanced
2:00remaining
Why does this findOne query return null?
Consider this query:

db.users.findOne({ age: { $gt: 50 } })

It returns null. What is the most likely reason?
AThe $gt operator is invalid in findOne
BThe query syntax is incorrect and causes an error
CNo document in the collection has age greater than 50
DfindOne always returns null if multiple documents match
Attempts:
2 left
💡 Hint
Think about what null means in findOne results.
🧠 Conceptual
expert
2:00remaining
Understanding findOne behavior with multiple matches
If multiple documents match the filter in a findOne query, which document is returned?
AThe first document according to the natural order in the collection
BThe document with the highest _id value
CA random document from the matches
DAn error indicating multiple matches
Attempts:
2 left
💡 Hint
Think about how MongoDB stores documents internally.