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