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


Talk Python's Mastodon Michael Kennedy's Mastodon