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