MongoDB with Async Python Transcripts
Chapter: Document Databases
Lecture: Querying Embedded Data
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I open this chapter by saying you could query into these embedded objects.
0:06
In our imagined bookstore here we have this book and it has the details like the title and ISBN, but it also has things like ratings.
0:15
Which user rated this particular book and what value did they give it? I guess zero out of ten or something like that.
0:22
So I wouldn't be able to answer the question with a native query. I want all the books that are rated by the user with this ID.
0:32
Object ID is the default auto-incrementing, auto-generated primary key if you don't do anything specific in MongoDB.
0:41
So you'll get used to object IDs that are everywhere. Now I want to know not just can I find a book, but I want to find all of the books in the
0:51
database that have in their ratings a user ID field that matches this object ID. How do we do that? Incredibly simple.
1:00
So we just go over here and we say find ratings.userID equals this value. Cool, right?
1:10
Now don't get confused that there's actually multiple values in the ratings that might have a value for user ID.
1:18
What it does is it looks here and it says we're going to go to the ratings list and
1:23
And then in that list, we're going to see if any of the things contained in there, the user ID happens to match this object ID that we specified.
1:31
So in this case, we might have 21 books, all of which somewhere in its ratings collection had this user ID value. I'm going to return that as a list.
1:40
Cool, right?