Query MongoDB Data
You can import your models into the Python interactive shell
to read data from the sample_mflix
database.
1
Query the "users" collection for a specified email
Start a Python shell by running the following command:
python manage.py shell
Then, run the following code to query the
sample_mflix.users
collection for a movie viewer whose email is
"jason_momoa@gameofthron.es"
:
from sample_mflix.models import Movie, Viewer Viewer.objects.filter(email="jason_momoa@gameofthron.es").first()
This code returns the name of the matching user:
<Viewer: Khal Drogo>
2
Query the "movies" collection for specified runtime values
Run the following code to query the sample_mflix.movies
collection for movies that have a runtime
value less than
10
:
Movie.objects.filter(runtime__lt=10)
This code returns a truncated list of the matching movies:
<QuerySet [<Movie: Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics>, <Movie: Steamboat Willie>, <Movie: Three Little Pigs>, <Movie: The Band Concert>, <Movie: Who Killed Cock Robin?>, <Movie: Dots>, <Movie: The Cat Concerto>, <Movie: Begone Dull Care>, <Movie: Mi adorado Juan>, <Movie: Neighbours>, <Movie: A Phantasy>, <Movie: Duck Amuck>, <Movie: Duck Dodgers in the 24èth Century>, <Movie: Blinkity Blank>, <Movie: One Froggy Evening>, <Movie: What's Opera, Doc?>, <Movie: Lines: Horizontal>, <Movie: Il fornaretto di Venezia>, <Movie: Dog Star Man: Part IV>, <Movie: Now>, '...(remaining elements truncated)...']>
After completing this step, you can run queries on data stored in your MongoDB deployment.