MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Basic classes in mongoengine
0:01 Alright, let's start defining our classes that we're going to map to the database. And I guess the first place to begin would be to describe
0:07 how I think we're going to store the data and what the data is; so we're going to have a car, a car is going to have an engine,
0:15 with lots of details about the engine like its horsepower and so on, a car is going to have a service history and each entry in the service history
0:22 is going to be some additional information, like what was the work performed, how much did it cost, when was it done, that kind of stuff.
0:29 There is going to be an owner who can own multiple cars and a car can be owned by multiple people,
0:36 so there's a many to many relationship between owners and cars, and then owners have personal information like their address and stuff like that.
0:43 So really the idea is we have cars and owners, and then the cars have additional things like engines
0:48 and then the thing you can do to the car that's really interesting is you can give it service right, change its tires,
0:54 change its parts and plugs, give it a new engine and so on. So we want to model those things, so let's start right at the heart of it,
1:00 let's start with the car. So over here, we're going to define another class, another Python file called car,
1:10 and we'll go down here and we're just going to define a class called car, like this. Now, we're going to need to work with MongoEngine,
1:20 because the way it works is all the classes, all the entities we want to map to the database are going to derive from mongoengine.Document;
1:28 now this allows us to load and save and query the documents, it also provides a field called id, which maps to underscore id in the database
1:41 and by default is an object id type of thing, okay so we don't have to worry about this id whether it's an object idea or not, you can change it
1:49 you can put a different one and overwrite it, but if you leave it alone this is what you get.
1:53 Okay, so the car now has an object id and we're going to give it a couple of pieces of information like about what model is it,
1:59 so if you've worked with these ORMs before, they are very similar, what we're going to do is we're going to define the properties of the document
2:07 as basically a descriptor, so it's a mongoengine. this is going to be a string, so we'll say string field
2:13 so you have sorted list field which is pretty sweet, we're going to start with a string field that's nice and easy, let's add while we're at it a make
2:21 so a model might be F40, make would be a Ferrari, we're going to have a year, mongoengine.IntField
2:29 now notice, we have types here, we have strings and we have integers in MongoDB, things have a type in bson they're strings or they're integers
2:37 but there is no way to enforce a type, there's no way to say the year must be an integer you could easily make it a list if you want it,
2:43 make anything you want, but in MongoEngine, it has a concrete type which is actually really valuable.
2:48 Let's have a mileage, and let's say the mileage is going to be a float field and then it's going to have a vin number, vehicle identification number
3:00 and that is going to be a mongoengine.StringField because it might have alpha numeric in it, it might start with zero, things like that.
3:08 Okay, so this pretty much is what we got to do in order to map this to the database. However, there's one more thing that you want to do,
3:16 so we're going to define this meta dictionary and the dictionary is going to say the database alias we want to use is core,
3:27 remember that from over here, we said this connection to this database with all the properties that we're not specifying because they're defaults
3:34 but we could have a server name, port authentication, all that kind of stuff we're going to say go find this connection that we've registered here
3:41 because the db alias we want to use is core; I find this a really nice way to partition our app up into like central parts
3:49 and analytics and reporting and those kinds of things. Then we can also control the name of the collections,
3:55 we don't want to be capital C Car, how about lower case cars. Alright that's more Pythonic for us, so we're going to call our collection cars,
4:02 and in the shell we would say db.cars.find, alright, but here we're going to work with MongoEngine.
4:08 So this is not the end game, but this is definitely the beginning, let's go down here and write some throw away code
4:14 just to see that we have everything hanging together. So let's go down, hit command b, so go to add car, and let's see what do we need here,
4:23 let's go and grab the stuff we're going to need, in fact, you'll see that some of these we're not going to have to set
4:28 especially when we get to the final version of what car looks like, but let's say we want to get the model, it's going to be input what is the model
4:39 I could almost just enter Ferrari because that's what it always is the make, so we have to ask the user some questions here
4:48 and I'm going to assume this is going to work, assuming that we can parse that as an integer and here we'll say mileage, that's going to be a float
5:09 and let's go and get a vin number. Okay, so now we want to create a car we want to insert it into the database
5:23 and later maybe even do a query with it, so we'll say car = Car like this and I could use keyword syntax to set the value here
5:29 let's go ahead and import that to the top, so I could say model equal such and such, year equals each and such.
5:36 Or I could say car.year = year, card.make = make, notice the auto complete which is very nice, model and we'll just keep going like this.
5:50 And then, in order to insert it, all we have to do is go to the car and say save
5:55 this is the active record style, in active record you work with a single document and you say load, save, query, things like that right,
6:03 you save them individually, which maps really well to MongoDB because it doesn't have concepts like transactions.
6:10 So let me just put in something wrong here for the mileage, remember the mileage, if you look over here, has to be a float
6:17 so let's try to put a string in there, all right so run my thing, I want to add a car, it is not going to make it through I believe,
6:24 so let's say 1998, it's going to be abc— it's going to crash, and it says car validation error no, no, no, the mileage only accepts floats and integers
6:40 so already in the simplest form of our car, I'm going to do a lot more to it it's already helping us out a lot here,
6:46 so oh yeah yeah yeah that was supposed to be a float, do you know how easy it is to make that mistake
6:51 when you're working in raw dictionaries and put in a string in the database when it should have been a float, and then how do you do a sort,
6:55 how do you do a greater than— you're out of luck, right so we already get a huge value from like the simplest variation.
7:01 Okay, let's go and put this in for real now, add a car, it is a F40, it's a Ferrari, it was built in 2005,
7:08 this time the mileage is 10 thousand miles, and the vin is af2. There we go, ready?
7:19 Oh it looks like I made a small mistake configuring MongoEngine here, let's go back really quick, that's unfortunate,
7:25 so over here if you look, I quickly typed in the alias and I said db
7:30 but no no, the thing that we want to use, the name of the database is not db its name, so sorry about that, let's fix this here,
7:37 all right, now let's do it again, now we should be able to add our car which we're going to go over here, we don't really need this anymore,
7:48 so we are going to ask for the input from the user, create the car and save it.
7:54 Now that it actually knows what database to use, that should be pretty easy.
7:58 So add the car, F40, Ferrari, 2005, driven slightly further since we crashed it and we tried to add it but here we go,
8:12 and this would be a F20, boom just like that, we've added it, remember, this demo dealership didn't even exist
8:21 until we just hit enter there, now let's see what we got, go back over to our favorite shell RoboMongo,
8:28 and now we have dealership which was already there but now we have demo dealership, and check this out
8:34 we have cars and in here if we look at it like this, there we have our model, our make, our year, our mileage and notice,
8:40 this is an integer, this is a float— why, because in the class that's how we defined it
8:45 and the other two obviously are strings, here is the underscore id that was generated or brought into existence
8:51 by being just mongoengine.document class, we didn't have to do anything to make that work.