MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Listing cars

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now it's time to service the car. So, we got a couple of options here, one possibility would just be let's get a service,
0:09 like a random car we grab from the database, the other would be to go over here and implement list cars.
0:14 So let's actually go ahead and do this list cars here, so we could hit list cars and then we can ask for the id or something to that effect of the car.
0:24 Ok, so let's go over here and talk about listing the cars, we haven't done any queries yet, everything we've really done so far is inserts
0:31 and that's really straightforward, right, we've seen that we create an object, we set some properties, they may be complicated properties
0:37 but nonetheless, we just set the properties and we call save. For listing or any sort of query, we're going to come over here
0:44 and we're going to do something different, so what we want to query are the cars, so we want to say car.objects,
0:51 now there's a couple of things we can do, we can come over here and we can say filter and set something
0:58 or if you're doing a single filter, you could actually do it right here, I'll do it the long way then we'll tighten it up,
1:03 I'm not sure there's really any benefit other than whatever you feel like typing,
1:07 do you want to be more explicit or do you want to have code be more concise. We could come over here and we could do a filter on the car,
1:15 we could say I want to see only the cars that are a particular year or something like that,
1:21 so I'm not sure we really need to filter it all, really all we want to do is sort them, so was say order by and then what we're going to do is
1:31 we're going to say the thing that we want to order by, and for order by, we use string, so let's order by the year
1:39 and let's just get with this for just a minute, so we'll say cars equal this, and then for car in cars
1:46 we want to print out, let's say the make, the model and vin, something like this. So here we'll say car.make, car.model, car. vi_number,
2:02 okay excellent, so that should print those out and let's do maybe a little extra line at the end.
2:07 Let's go ahead and test this, and see if everything is working, so let's list the cars, excellent, surprise it's all Ferraries,
2:13 remember, we are a Ferrari dealership, and this is not super helpful because it doesn't show us the year,
2:19 I want to show you that the order is working, and if you don't see that you're not going to be able to verify whether this is working or not.
2:27 So let's do this, we'll come over here and we'll say car.year, do it one more time, now we list, all right look at that,
2:33 so 1991, 2000, 2005, 2005 and 2017, perfect it's working, but I'd kind of want to see the new ones first,
2:41 although maybe in terms of service, seeing the ones is what we want, let's say we want to get them in reverse, so put a negative here,
2:51 here we go, now we're sorting the cars, newest to oldest, sorting by year, descending, okay, so that's working really well,
2:58 so what we want to do is basically use this vin number, to go find the car we want to service,
3:04 now that we can see the cars I'd like to go say I want to service a car and come over here hit s, and it is supposed to say okay
3:10 what car do you want to service, I give it one of these, we're going to go to the database, find that car and then insert a service record to it.
3:17 With that mind, let's go and think about this here, let's think about actually showing the service history of each car.
3:26 Some number of service records like that, format what we want to print out is a len of car.service history
3:35 now this is as far as we're concerned in memory just a Python list, so we don't need any database magics,
3:41 like a straight up length will tell us what we're looking for. And then for each one of these, we can go and look at it
3:46 so as in card.service history, this doesn't go back to the database a second time,
3:50 that's part of the beauty of just pulling it back as part of the document so here we'll print, we'll do like an indent, something like this,
3:57 it'll have let's say we're going to just show the name, let's do the price and the name, I think that might do it.
4:05 All right, so we're going to s.price, s.description, I believe is what we called it, let's go and run this,
4:14 we should see just zero histories everywhere, zero service records so not super interesting,
4:20 but it should become interesting as we write this next thing, Alright, so now that we can find the cars, the next thing to do is actually service them,
4:28 let's go ahead and write one more query here, and then I'll carry on, okay, so first thing we want to do, is we're going to get the vin,
4:34 I'll say input, now in reality, the car would be sitting in front of us, here we don't really know, so we need to find a way to get the car right,
4:49 so let's say this, we want to go to the car and we'll say car.objects now here's where we really actually are doing the filter stuff
4:56 so we'll say filter— and this is pretty cool, we're going to go to this and we're going to basically pretend this function has named parameters
5:05 that in the simple version represents the prototypical json objects, we saw from the api, from the Javascript and PyMongo api.
5:15 So let's go to the car real quick, what is the thing we're looking for, vi number is what I called it, so we're going to say that equals to vin,
5:23 if we wanted to also match the make = make, right model = model, whatever, if we had three things
5:29 we want to do like an and on all three, this is how we would do it,
5:33 but we don't, we just have the vin, now this will give us a list of cars that match this, in theory, this should be unique, we haven't got the indexes
5:40 or any other way to make them unique yet, but we can get there, now we're expecting one back so we can come here and say first,
5:47 and that should actually pull back the one car so instead of getting the cursor, we're going to get either a car or none.
5:52 If we had none, that means it wasn't found, so let's say if not car, print car with vin not found,
6:08 okay so a little bit of error handling there, this doesn't throw an exception if it misses,
6:12 it's just going to be none, all right, so then we'll say print we will service let's say car.make, let's see if this works,
6:22 okay so list cars, let's go, let me put in service, say bad vin, car with bad vin not found, excellent
6:32 let's try to service this one that has this vin that I copied, boom, we will service Ferrari excellent, excellent,
6:38 so we'll go into the database and get it. Now that doesn't really prove very much, does it, that this is the model, there's only one Testarossa,
6:51 so if I do this, it should say we're going to service the Testarossa, perfect,
6:57 all right, so now, we have everything working beautifully in terms of our filter here,
7:02 now I told you there's two ways to do this, if we have multiple clauses and you want to do multiple filters and multiple ifs and conditionals,
7:09 just keep piling on the filters, but if you want a simpler version you can actually do it like this, as well, and let's just verify.
7:15 Boom, see, we're going to still service that same car. So now that we have it, how do we get this service record onto the car,
7:28 you'll see there's actually two ways, and they're both pretty decent.


Talk Python's Mastodon Michael Kennedy's Mastodon