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

Talk Python's Mastodon Michael Kennedy's Mastodon