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