MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Concept: Creating classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The way we primarily work with MongoEngine
0:03 is we create classes and we map those to collections.
0:06 So here we started out with a really simple car,
0:08 we have a class called car and anything that maps to a collection
0:13 is a top level document must be well derived from mongoengine.document
0:18 and then we set up just all of the fields, these could be simple
0:21 or as we saw they could be nested rich objects,
0:24 all the ones listed here are simple, so we have string, string int, int and string.
0:29 So we just do that mongoengine.stringField and so on.
0:31 So this worked pretty well, but we said it would be nice
0:34 if we could express that some of these are required,
0:36 that some of these have default values and things like that,
0:39 so we can come in here and we can say the model, the make, and the year
0:43 these are all required, just say required = true you must type them in;
0:47 mileage, we might be happy to go with zero for default
0:50 this is new cars, things like that, so zero is a good default there,
0:54 the vi number, the vin number is more interesting,
0:57 we want to generate a large unique alpha numeric string
1:01 automatically when a car is created,
1:03 so we'll say default equals and will give it some kind of callable
1:06 in this case a lambda that returns a string based on taking the uuid4,
1:11 turn it to a string, drop the dashes, things like that.
1:14 So this worked really well for generating our car
1:17 and we didn't even have to set the vin number, that just got done automatically.
1:21 Finally, we said look, our cars also are going to contain an engine
1:24 and I don't want to go and do a separate query to a separate table
1:28 or separate collection specifically,
1:31 to find out details about the engine and store like the car id in the engine,
1:34 so instead, we're just going to embed it straight into the car,
1:38 you have a car, you have the entire details, precisely.
1:40 So we did that by first creating an engine class
1:43 and that engine class has to derive from mongoengine.EmbeddedDocument
1:48 not document, don't make that mistake, EmbeddedDocument
1:51 and then we're going to set the type of it here in the car
1:53 to be an embedded document field,
1:56 the embedded document feel takes two things,
1:57 the type that you're going to put there so the engine class
2:00 and whether it's required is optional, right,
2:03 but we're going to say at least here yes the engine is required.
2:06 We also wanted to store the service history,
2:08 a set of rich documents modeled by service records,
2:11 so again here's a class derive some embedded document
2:13 but this time it's not one thing, it's a list of them,
2:16 so we have an embedded document list field
2:18 and this basically starts out as an empty list
2:21 and then as we wish we can append these service records to it
2:25 and then save them back.
2:27 So if we have our car model like this and we put one into the database
2:30 it's going to come out looking like this,
2:32 we'll have an id, we'll have a model,
2:34 bunch of other flat elements up there, flat fields
2:36 we have our vin number generated as 9501, from that lambda expression,
2:41 the engine has four properties horse power, liters, miles per gallon, serial number,
2:45 and that is modeled by that engine object,
2:48 and notice the curly braces, this is an embedded sub document here
2:52 and the service history, notice square bracket this is a list or an array in Javascript
2:57 and it has a bunch of sub documents that are the service history.
3:00 So with our car modeled in Python on the left
3:02 what we get here on the right is actually what you'll see in MongoDB.