MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Concept: Basic querying

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We've explored the shell a little bit, we've done some querying, let's look at the concepts behind it, so you have them nice and concise,
0:07 in case you want to come back for a reference. So if we want to query say the Book collection in the bookstore database
0:13 where the title is 'From the Corner of His Eye', we can type find and give it this little prototypical json object,
0:21 hit enter, and boom everything comes back that has the same title, different isbns, different primary keys and so on, but releases, different versions,
0:30 maybe one is paper back on is kindle, who knows; so the idea is we're going to come up with these prototypical json objects,
0:36 here title: whatever the title is. Now, if we want to do more than just what is the title here
0:43 we want to say give me the book with the title this and the isbn that, given that the isbn is probably unique,
0:51 we could maybe just search for it instead, but we want to demonstrate the and clause, right. So here we'll give it this prototypical sub document
0:57 with the title being the title we're looking for, and the isbn being this one. And notice, now we only get one record back,
1:04 so our prototype will document is basically an and clause, every field must match. We also saw that one of the excellent ways to group related data,
1:15 this would be what you might call an aggregate in domain driven design, is to embed items into the document,
1:23 so here we have ratings that ratings have little sub objects, sub documents that have things like user ids and values
1:29 and at the very beginning, and in the example you saw, the superpower of these document databases, is that they can query them,
1:35 so I want to find all the books that have been rated by this highlighted user id— how do I do that? So we just pretend we're traversing the objects
1:42 Ratings.UserId, so down here we'll say find Ratings.UserId and we give it the object id that we're looking for
1:50 because ''Ratings.UserId'' is not a valid key or a field name in a Javascript object
1:55 we have to put it in quotes, but other than that, it's basically the same idea
1:59 and here we get back all the books that have been rated by this particular user. So we just use this dotted notation to traverse the hierarchy
2:08 one other interesting point is maybe ratings just contained the number like it was at 7,5,... then you could actually just if I want to say
2:18 find all the books that have a rating of seven I could just say find ratings:7, I don't have to do this dot notation or anything like that,
2:26 but because I'm looking within that document inside ratings, regardless of whether it's an array or it's a single rating thing,
2:32 you do it like this that dot notation.

Talk Python's Mastodon Michael Kennedy's Mastodon