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

Talk Python's Mastodon Michael Kennedy's Mastodon