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