MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Subdocument queries
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So we've really explored a lot of MongoEngine and we've built upon the foundation that we laid with the Javascript api
0:08
and transferring that over to the PyMongo api; so hopefully, nothing you've seen has surprised you in terms of the types of queries that we're doing,
0:17
it's just learning how MongoEngine surfaces that and turns it into objects was really what we were looking for;
0:23
now, there are a few other things that we need to talk about that really we haven't touched on yet,
0:29
the operators, we talked about the atomic update operators but not things like the greater than, less than, exists, doesn't exist,
0:37
in set and so on, so we want to look at that; we also want to look at querying into subdocuments
0:45
so if we go back to our MongoEngine here, we'll run this one more time, see, maybe we want to ask questions like
0:52
show me the cars that have had some either really good service or really bad service, so we want to query all the way down into service history,
1:00
into customer rating, and do a question like show me the ratings that were 5, show me those the ones that were 4,
1:06
show me the ones that are less than 3, things like that, so how do we do this in this format that MongoEngine uses?
1:12
So I've sketched out this little 'show poorly serviced cars' and it doesn't do anything, it just pulls back every car
1:18
and prints it more or less like we had before, except for it shows the satisfaction in addition to the other stuff;
1:23
so the question is how do I query it, let's just run it real quick, and I can say show me the poorly serviced cars,
1:29
it doesn't matter what we put now, and it literally just lists all of them, and notice this one has a satisfaction of 3, 3, 5 and 3,
1:35
so that we can do some queries, let's work on two other cars, let's work on the Ferrari 308 and this 2017 F40.
1:43
So let's perform some service on this one and let's say this one got some amazing service, the price was 12 dollars,
1:53
and we have a let's say monthly check up again here spelled right even, and they were just thrilled,
2:02
so let's do our list really quick, and now, notice this one had a very happy one in fact, if I say the poorly serviced cars for a moment
2:12
it's going to show that this one had a satisfaction of 5, okay let's suppose the 308 is not having such a good day, let's service it,
2:20
and let's say that its price was 10 thousand dollars, the type of service was fender dent repair,
2:28
so maybe the family went out of town and the teenage son stayed home, the son took the Ferrari out, found the keys and crashed it,
2:36
so you can't blame the guy for being unhappy, but you know, what are we supposed to do, he came in unhappy,
2:42
we tried to make him happy, but he was just not having it, so he had a 1, and now let's look really quick, just list everything still,
2:49
so you can see over here, this Ferrari has no records, this one, this F40 2017, was very satisfied,
2:57
the 308 very unsatisfied, and this Testarossa has some that are satisfied. Okay, so great, now we have the right variety of data,
3:06
let's go over here and write the code that we were trying to write in the first place.
3:10
What I want to do is I want to find the cars that had great service, so that's pretty easy to do, we saw that we could do like
3:16
vi_number = 7, but what about, over here— remember what we want to do, find the one with lots of them,
3:30
we want to go into service history and down into service history, we want to find customer rating, how do we do that in this format?
3:37
Well, it starts with this, service history, and what's the thing called down here, just do a copy to be sure it's identical,
3:45
because you don't get an error if you get it wrong, just no results. So I told you that double underscore has special meaning
3:49
we used it for the push operator earlier, we can also use it here to traverse the hierarchy, so service_history_ _customer_rating
3:58
is going to go down and let's say this is going to match whatever level they passed in, all right, let's try this.
4:04
So I want to find poorly service right now it assumes that we're going to enter a low number, but let's just run with it for a minute,
4:13
let's say I want to find the ones with level 1, all right, so it was this Ferrari 308 here, and I think that's the only one that has level one,
4:22
let's go and run the poor but ask for 5, so like I said, bad name, servers at a level or whatever;
4:29
now we have two, right, we have this Ferrari F40 with this here, and we have the Testarossa, which some of the time
4:37
at least had really good service, the person was super thrilled. So that's how we search into those subarrays, we used the double underscore,
4:46
so double underscore we used it for push onto a thing, we use it to navigate a hierarchy, the last thing that we really are looking for is
4:54
we would like to find the cars that say have below excellent service or something like that,
4:59
so let's change this a little bit, max level of satisfaction are we looking for; so we could say 1 and that's a really bad one,
5:10
if we could say 3, and we could intend that to be 1, 2 or 3, as the level, right,
5:15
so it's not going to work this way now, it's just going to be straight up a quality. So, once again, how do we do it in the Javascript api or PyMongo—
5:25
we would use something like this, we would say that, we would say service_history.customer_rating
5:39
and then here instead of giving a number we would give it one of those operators, we would say $ lte (less than or equal to) : level, right
5:50
so how do we do that here— well, we want to use this operator and we're going to do that again with the double underscore,
5:59
so we'll say double underscore __lte, but here's the thing, the query operators go on the end,
6:04
the update operators go on the beginning, remember push was like this so the order varies, for better or worse,
6:11
I think it has to do with the fact that the operators here go to the right in the raw api,
6:16
and the push one goes to the left, so it's kind of trying to mirror that. All right, let's run this again. So let's see the poorly serviced cars,
6:26
let's try again for 1, we should see just the 308 because that's the only one with that level, boom, there's the 308.
6:32
Let's look for it again, I want to find all the cars with 3 or below,
6:36
remember, if I scroll this up a little bit, we're doing lte less than or equal to 3, bam, look at that, we got the 308 and we got the Testarossa,
6:46
which some of the time did have this, all right, if I put 5, we would just get all of them.
6:51
So you can see that we can use the double underscore to traverse the hierarchy, we can use the double underscore for the operators,
6:56
and in fact, we can use the double underscore for multiple meanings in the exact same thing, right here, traverse service_history.customer_rating
7:05
and then apply the operator less than or equal to the value that we set.