Retrieving a specific document using .find_one()
Where .find()
returns a cursor to all documents matching a filter query, .find_one()
returns the first document that matches the filter query. This is useful for specific use cases (like looking for a specific movie), as well as for limiting how many results the MongoDB server returns.
To refresh your memory on all available fields in the movies collection, the first document in the collection has been printed for you.
This exercise is part of the course
Introduction to MongoDB in Python
Exercise instructions
- Find the movie with the title
"her"
(casing matters!) and store the resulting document as the variableher
. - Find a movie that has
"adventure"
among its genres and store it as a variableadv
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Find the movie with the title "her"
her = mov.____
print(her)
# Call the find_one() method on the movies collection
adv = mov.____
print(adv)