Recall & Review
beginner
What does the
findOne method do in MongoDB?The
findOne method searches a collection and returns the first document that matches the query criteria. If no document matches, it returns null.Click to reveal answer
beginner
How do you use
findOne to find a user with the name 'Alice'?You call
db.collection.findOne({ name: 'Alice' }). This returns the first document where the name field equals 'Alice'.Click to reveal answer
intermediate
What is the difference between
findOne and find in MongoDB?findOne returns a single document (the first match), while find returns a cursor to all matching documents.Click to reveal answer
beginner
What does
findOne return if no documents match the query?It returns
null, meaning no matching document was found.Click to reveal answer
intermediate
Can you use projection with
findOne? What does it do?Yes, you can pass a projection object as the second argument to
findOne. It controls which fields are included or excluded in the returned document.Click to reveal answer
What does
findOne return if multiple documents match the query?✗ Incorrect
findOne returns only the first document that matches the query.How do you exclude the
password field from the result using findOne?✗ Incorrect
Use projection with
{ password: 0 } to exclude the password field.Which of these is a valid
findOne query to find a document with age greater than 25?✗ Incorrect
MongoDB uses
$gt operator inside an object to specify 'greater than'.If no document matches the query, what does
findOne return?✗ Incorrect
findOne returns null when no match is found.Which method would you use to get multiple documents matching a query?
✗ Incorrect
find returns a cursor to all matching documents.Explain how to use the
findOne method to retrieve a document by a specific field value.Think about searching for a user by name.
You got /4 concepts.
Describe how to use projection with
findOne and why it might be useful.Consider hiding a password field in the result.
You got /4 concepts.