MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Concept: Querying with mongoengine

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw querying in MongoEngine was really quite straightforward
0:04 and mostly was done by way of interacting with the class
0:07 and the arguments, not breaking down and working
0:12 in the actual MongoDB or even PyMongo api;
0:15 so let's look at a couple of options,
0:17 a couple of ways in which we might do some queries.
0:19 So here we have a function called find_car_by_id
0:21 and notice it's taking a car id which we're using type annotations or type ins
0:25 to indicate that this is an object id that comes in
0:28 and what we give back, what we're returning is an individual single car
0:31 and really for the type ins to be entirely correct
0:34 you should say optional car, because it may be none as we saw.
0:37 So we are going to say car.objects and then filter(id = car_id)
0:41 so what we're doing is saying we're looking for the car object
0:44 that has id = car_id, now this is by primary key basically
0:48 so you expect it's one or zero, we do a first
0:51 so that actually returns either the object or none.
0:54 One thing to know is in the database it's _id and
0:57 in MongoEngine they are like forget the underscore it's just straight up id,
1:01 so minor difference there,
1:03 we also saw that if you have just one filter, like a really simple thing
1:06 you could just say objects_id = card_id
1:08 and don't have to do the filter step, but I kind of like this explicit style.
1:11 So we're going to say we're looking for one or more fields
1:16 we're only passing id but we could pass id and vin number and other types of things
1:21 and we call first to get one or nothing at all,
1:26 next, we might want to query by subdocuments
1:29 or things contained in a list inside of that document, stuff like that;
1:33 so we can also do this with MongoEngine.
1:36 Same type of thing card.objects.filter, however what goes in here
1:40 is no longer just the straight name,
1:43 in fact, we're going to use the double underscore
1:45 to traverse that hierarchy, so we're going to go down to service history,
1:50 then within service history we're going to look at customer rating.
1:54 Now, if we're going to return this, we maybe don't want to return it active cursor
1:59 we may either want to use a generator
2:02 or here what we're doing is we're actually creating a list
2:04 we're saying list of cars, that way by the time this function is done executing
2:09 it actually is going to entirely have finished whatever it's doing with the database
2:14 and you'll basically be done with the database by the time you leave this function.
2:19 Notice we're also using type ins to say this takes a list of cars,
2:22 this time we didn't call first we're just converting all of the responses to that list.
2:27 All right, finally this one that we just looked at
2:31 was looking exactly at the customer rating of 4,
2:34 but here we want to know like show me all the cars
2:37 that were not rated with great service, right
2:39 that is 3, 2, 1 or 0, if that's a possibility,
2:43 and in fact, we're going to use not pull all the cars back
2:47 but we want to know like as a percentage how are we doing,
2:50 how many cars that we have that had sub amazing service, versus all of them
2:55 so we're using the count operator here,
2:57 we can get all the cars by saying card.objects.count
3:00 or we can run our query and say count and get just the ones that match the query.
3:03 So in this case, we're going to say you the double underscore yet again
3:07 this time to use the less than operator,
3:09 so what we're saying in this case is the bad cars how many are there,
3:13 well, we're going to go to service history.customer rating
3:16 and show me all the ones that are less than 4
3:18 and count how many of those occur.
3:20 Right, so we'll just use the count operator
3:23 instead of actually returning deserializing the documents
3:25 this is much, much faster than saying give me all the bad cars
3:28 do a lin operator, unless you have a really, really great service.