MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Adding the engine to the car document

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So we have the primary properties of our car modeled,
0:04 they have their required fields, they have their default values, things like that.
0:08 We still maybe want to consider indexes
0:10 but we're saving that for a performance area.
0:12 The next thing we want to look at is the engine, and the embedded elements.
0:16 We talked about at the beginning that the car is going to have an engine,
0:20 and it's going to be equal to something,
0:22 not a string or a float or something like that,
0:25 but in fact, to an entire subclass, right,
0:28 a class that represents engines in particular,
0:30 so let's create that class and then we'll come back to the car.
0:33 So if we come over here, I'm going to create something called engine
0:37 and just like before, we are going to import MongoEngine,
0:41 not the same engine, right, class engine
0:44 and this is going to derive from MongoEngine document,
0:46 now, before I said document has the id
0:49 and this is like the top level thing that allows saving and loading,
0:51 so we don't use this type for embedded documents, subdocuments,
0:56 right, subdocuments don't necessarily have ids,
0:58 you don't load query and save them etc independently,
1:02 you can only work with them through their parent document,
1:05 so in this case, we are going to say this is only allowed as an embedded document
1:08 it can't be queried or saved directly, but it can be used
1:11 as a subelement of another type, like for example our car.
1:17 So let's go over here, and give our engine a couple of properties,
1:20 we're going to give it the horsepower
1:23 and the horsepower is going to be a mongoengine.integer,
1:28 so this is going to be an int field, it is going to have a leaders
1:31 so the size of the engine and this will be mongoengine float field
1:35 because it could have like 2.3 liter engine something like that;
1:40 we'll have the mpg, so miles per gallon,
1:44 and this is going to be the same thing, a float,
1:46 finally it'll have a serial number, and this is going to be a mongoengine string field.
1:55 And the serial number is kind of like the vin number,
1:58 but in fact it's not exactly the same, it's going to be having dashes
2:03 it will have a slightly different format, but let's go ahead
2:05 and work on some of the default values in that,
2:08 so we're going to import uuid, I am going to use just again uuid
2:12 to actually generate this so quick review, default is a lambda,
2:16 and the lambda is going to return a string representation of uuid4,
2:22 I believe the dash is in there this time, just to make sure
2:24 hey this is clearly not a vin number, it's a serial number.
2:28 And let's set these all to be required, so we can have in the subdocument itself
2:34 these required values, except for the serial number,
2:36 which is going to be autogenerated;
2:38 all right, so let's go back now that we have this not a document
2:41 but a subdocument, an embedded document, let's go back to the car
2:45 so in the car what am I going to set this to, a MongoEngine something,
2:48 right so this is not going to be a string or float or anything,
2:52 it's going to be an embedded document field,
2:54 right, so we have an embedded document list field or just a field
2:58 so this is a single engine, not a list of them, so here it goes like that
3:02 and then I need to tell it not just what goes in there, but the document type
3:06 what is the subdocument, the subdocument is actually this,
3:09 and then let's go ahead and say required = true
3:12 so we could even say that this subdocument cannot be
3:15 none or null in Javascript, it has to be set to a thing.
3:18 So, we're going to import the engine, so it knows the car,
3:22 it knows about the engine and it can save it there,
3:25 all right, let's try to work with our engine here and see what happens.
3:28 Okay, it runs, that's already pretty encouraging,
3:31 so the model is going to be the Testarossa,
3:34 I'm sure that's misspelled but we'll roll with it,
3:36 Ferrari, it was built in 2010 and this is going to crash
3:41 because the engine was not specified, how do we do that,
3:44 well let's jump right where the problem is and find out.
3:48 So we need to set the engine, now I'm just going to hardcore engine setting
3:52 so we don't ask this anymore, right, so we want to come over and say
3:56 allocate an engine, allocating it is just the same as the top level item
4:01 we'll say engine.horsepower is around six hundred horsepower,
4:04 that's pretty insane isn't it, we have the miles per gallon,
4:08 I think that's around 20, not super high, liters let's say 5.0
4:17 it's not exactly right, I'm sure but close enough;
4:20 and then we just say car.engine is engine, like this,
4:24 so we create this object and we associate it,
4:26 and then later we can say car.engine. and we get all the various things here.
4:30 Okay, so here we have our car, we set the engine and now let's do this again;
4:35 in fact, let me just comment this out a little bit,
4:42 yeah, we'll just ask those two questions, keep it little simpler.
4:45 Ok so we're going to add a car, the model is Testarossa,
4:49 don't think we have one yet, let's open up our shell,
4:53 we have the couple F40s and the 308, but no Testarossa,
4:59 2005, and boom, inserted, okay let's run this again.
5:06 Where did it go, oh there it is, check that out how awesome is that!
5:10 So we have our Testarossa 2008, the mileage defaulted to zero,
5:14 the vin number was autogenerated, now here we have our engine
5:18 and check this out, here is our subdocument
5:21 curly brace means object subdocument in json
5:24 so we have horse power 600, liters are 5,
5:26 miles per gallon is 20, serial number is such and such,
5:30 let's go and make a quick change here so we can ask some interesting questions,
5:34 let me make this 22, this is a more efficient version
5:38 and it only makes 590 horse power, okay let's just insert one more.
5:43 So we're going to add what model, so let's say 355
5:47 is that a thing, I'm not sure, 2000, like so.
5:51 So if we go over here and run this, now we have these two
5:57 that have engines, this one has a 590 and so on,
6:00 so we can actually go over here and ask interesting questions
6:03 like I want all the cars with an engine where,
6:06 let's go for liters, is we can say something like $ > what value, say 5.95
6:15 and of course, got to close everything off,
6:19 what did I miss— I missed something didn't I, because it didn't come back,
6:24 horse power could be that much or liters could be something much smaller
6:28 so here, horsepower this much, or liter, I could have done 4.5,
6:33 there you go, so now you can see that we can query down into this subdocument
6:37 but it's going to get more interesting when we start doing queries with MongoEngine,
6:40 because we want to get these rich objects back.