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

Talk Python's Mastodon Michael Kennedy's Mastodon