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,
0:03 let's look at the concepts behind it, so you have them nice and concise,
0:06 in case you want to come back for a reference.
0:09 So if we want to query say the Book collection in the bookstore database
0:12 where the title is 'From the Corner of His Eye',
0:17 we can type find and give it this little prototypical json object,
0:20 hit enter, and boom everything comes back that has the same title,
0:25 different isbns, different primary keys and so on,
0:27 but releases, different versions,
0:29 maybe one is paper back on is kindle, who knows;
0:31 so the idea is we're going to come up with these prototypical json objects,
0:35 here title: whatever the title is.
0:39 Now, if we want to do more than just what is the title here
0:42 we want to say give me the book with the title this and the isbn that,
0:47 given that the isbn is probably unique,
0:50 we could maybe just search for it instead,
0:52 but we want to demonstrate the and clause, right.
0:54 So here we'll give it this prototypical sub document
0:56 with the title being the title we're looking for, and the isbn being this one.
1:00 And notice, now we only get one record back,
1:03 so our prototype will document is basically an and clause, every field must match.
1:09 We also saw that one of the excellent ways to group related data,
1:14 this would be what you might call an aggregate in domain driven design,
1:18 is to embed items into the document,
1:22 so here we have ratings that ratings have little sub objects,
1:25 sub documents that have things like user ids and values
1:28 and at the very beginning, and in the example you saw,
1:30 the superpower of these document databases, is that they can query them,
1:34 so I want to find all the books that have been rated by this highlighted user id—
1:38 how do I do that? So we just pretend we're traversing the objects
1:41 Ratings.UserId, so down here we'll say find Ratings.UserId
1:46 and we give it the object id that we're looking for
1:49 because ''Ratings.UserId'' is not a valid key or a field name in a Javascript object
1:54 we have to put it in quotes, but other than that, it's basically the same idea
1:58 and here we get back all the books that have been rated by this particular user.
2:02 So we just use this dotted notation to traverse the hierarchy
2:07 one other interesting point is maybe ratings just contained the number
2:11 like it was at 7,5,... then you could actually just if I want to say
2:17 find all the books that have a rating of seven
2:20 I could just say find ratings:7,
2:23 I don't have to do this dot notation or anything like that,
2:25 but because I'm looking within that document inside ratings,
2:27 regardless of whether it's an array or it's a single rating thing,
2:31 you do it like this that dot notation.