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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now that we have our engine stored in our car
0:04 over here as an embedded document,
0:06 the next thing that we need to work on in our car model
0:08 is how do we store the service histories,
0:11 first of all, what kind of data are we going to have in the service history itself.
0:15 So let's go create a class that represents that
0:17 and then we'll figure out what to do with it.
0:20 Again we're going to import MongoEngine, create a class called service history
0:27 and we're going to postpone discussing what goes in there for a minute.
0:32 So this is going to have a date, where this has happened
0:36 either like a create a date or the date when the service was done,
0:39 so let's create a MongoEngine, a date time field and let's even set the default to now,
0:45 so we'll go over here and say—
0:53 so we want to set this to be a lambda,
0:55 actually we don't need to set it to a lambda,
0:57 we'll just set it to datetime.datetime.now without parenthesis,
1:01 we don't want to call it, we want to just pass that function,
1:04 so we're going to call the now function whenever something gets inserted
1:07 so the date we could even call this like service date if we want,
1:10 but I'm going to stick with date.
1:13 The next thing is let's have some kind of description here
1:15 like just some text, we'll say description,
1:18 it's going to be a mongoengine.StringField,
1:22 and it is just going to be like changed the oil, they had a flat tire,
1:26 a nail was stuck in it, we patched the tire and everything was good,
1:30 something like that, right super simple;
1:32 we have a price, this is how much they paid for the service,
1:35 so it will be a float field, and lastly we care about our customers
1:38 we're primarily a service shop and sometimes we sell our Fearraries
1:43 and sometimes we just service them, but we want our customers to be happy
1:47 and how do you know whether they're happy— we better ask them,
1:50 so let's ask about their customer rating
1:55 and this is going to be an int field, so we're going to set this
1:58 this number is going to go from let's say one to five
2:03 five being the most satisfied, one being the least satisfied.
2:07 Great, so now here's the deal, do we want to embed this into the car
2:12 like we did the engine or do we need to come over here and say something like this
2:17 car_id = mongoengine.ObjectIdField, like this, right
2:23 so we're going to have a relationship over to the car
2:25 or maybe the other way around, on the car, we could have some bunch of ids
2:31 that represent the service history, or there's a bunch of other options.
2:33 So remember when we're designing our documents
2:36 one of the primary questions is in our application
2:39 do we want that embedded data with us, most of the time
2:43 and it turns out because almost all of our data work
2:47 are reason to pull up one of these cars is to actually look at the history of it
2:51 we are going to decide for that reason, that we do
2:54 almost always want the service history associated with the car
2:57 and we don't usually need the service history without the car,
2:59 we need details about the car like the mileage for example.
3:02 How are we going to do all that— let that means
3:06 we probably want to embed the service history as an array into this car;
3:10 the other thing we have to care about is
3:13 is that set bounded and is that bound small?
3:15 You know, a car how much could it possibly get worked on,
3:18 right let's say it gets worked on once a month every month, just ongoing,
3:24 very very unlikely, but that would give us at most a hundred of these service histories
3:28 let's say for some reason that like that upper bound is totally fine with us,
3:32 it's certainly not crazy unbounded where it's going to escape the 16 MB ram
3:36 I mean, how much could one these service histories be, 2 K each, not a huge deal.
3:40 So for all those reasons we are deciding to embed the service history into the car
3:45 so we want to come over here just like we did with engine
3:47 I'll say service history, could be plural could be singular, let's go with singular
3:51 so I'm going to go mongoengine, now it's not an embedded document field
3:54 because this is not a single document, this is a list,
3:57 so instead, is we are going to have an embedded document list field,
4:01 now over here in this one, we said what is the type that is the embedded document
4:05 here what we can say is what things, what type of things are contained in that list
4:10 so this will be a service history, we've got to import that, thank you PyCharm,
4:16 and then we could come over here and we could say
4:19 the default value is like a new array or something, a new list
4:22 but in fact, that's what it already is,
4:24 so the default value for embedded document list is a new list for each entry,
4:29 so we're just going to go with that, that seems totally good to us.
4:32 All right, so now we have this mapped, let's actually go back to our app
4:36 and add the ability to create and insert some histories.
4:40 One thing that we almost forgot, since we decided
4:43 we're going to embed this service history,
4:46 that tells us how we need to treat the base class for the service history.
4:49 So recall for the embedded items,
4:52 this is going to be a mongoengine.EmbeddedDocument,
4:55 if it was going to be standalone and in its own collection it would just be a document.
4:59 There we go, now our service history is ready to go.