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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Okay, so we can list the cars, we can now go and find the individual cars
0:04 using this query and a little bit of error handling here.
0:08 Now, let's go and actually add the service record
0:10 so we are going to create a service = service record
0:16 I think this is what we called it, no, service history, sorry,
0:24 we're going to import that, so we're going to allocate one of these
0:27 and we just need to set a few properties, the price is
0:29 let's just straight up input this— what is the price right,
0:35 maybe we would look up at the type of service
0:37 and it would have an associated price
0:39 but this is just a demo, right, just trying to get it to work;
0:41 so this is the float, some kind of currency thing, I want to say service.date,
0:48 we don't need to set the date, this should happen automatically,
0:50 description is going to be input what type of service is this,
0:56 and then the next one we want is going to be the customer rating,
1:02 and this is an integer, how happy is our customer, I want to put a one to five,
1:10 something like this to remind them that that's the scale they get to work with.
1:14 Okay, we're just assuming this is parsable by float,
1:17 this can be an integer right, we're not dealing with bad input.
1:21 So that should work, let's just double check the service history
1:24 these three we're setting, this one has a default, perfect.
1:27 Ok, so finally all we have to do is we don't insert the service history directly
1:32 because the service history is not a top level object
1:36 it doesn't belong in its own collection,
1:39 instead what we do is we go to the car and we say service history
1:42 it's a list, so we're going to put it on it like this, like so,
1:45 the ones we've changed the car we need to push that to the database like this,
1:49 so that's all we got to do, we're just going to put the service history here and insert,
1:54 let's go and run this and see what we get,
1:57 alright, once again we're going to list the cars
1:59 and notice we're going to work on the one that is really obvious
2:01 this 2005 Testarossa, okay so let's service it,
2:03 here's our vin number, excellent we're going to service it,
2:06 what is the price of the service— this is expensive, 15 thousand,
2:10 this is going to be a new engine, they blew the engine
2:13 a customer was very happy, normally, new engine is 20 thousand
2:17 and so we got him a good deal— bam, just like that, we've inserted it,
2:21 let's try to list it I might still have that format wrong;
2:24 no I got it sort of right, so we definitely got that to work there,
2:29 let me just change that format like I said,
2:35 there we go, actually let's just drop the currency, I'm sorry drop the decimal points,
2:43 so here 15 thousand dollars, for our new engine,
2:46 look at that, it's in there, let's go and actually do another service on that same car
2:50 the price of this one is a 102 dollars, this is going to be a flat tire,
2:55 and the customer was a little grumpy, felt like we took too long to fix the car
3:00 but they still like us, they give us a three;
3:03 so now if we list it you can see now there's two service records on the Testarossa,
3:07 pretty cool, right, that's super easy, and we don't need to do a join to get this information,
3:11 it comes built in even if we're doing horizontal scaling across ten shards it's all there,
3:17 let's go look at Robomongo,
3:21 it's a little small, what do you think, it does that sometimes, I don't know way,
3:25 okay, here is our demo dealership, let's view the documents
3:28 and we can say vi number, now of course we don't really need to do this,
3:33 but we can, just to show you, we have tons of data,
3:37 we look down here and now check this out, is that cool,
3:39 so we've got our flat level things, here's the vin we just did a search by
3:43 we have this embedded engine we already saw,
3:45 we have our service history and moreover our service history is setting the date
3:48 right, so right now it's like eight o'clock 04 seconds and 56 seconds right,
3:53 the price of this is beautiful, just beautiful,
3:57 so now if we do a query against this,
3:59 we go and say show me the vehicle with this id number,
4:02 we're going to automatically get the details about the engine,
4:04 we're going to automatically get the details about their service history,
4:07 without going back to the database or doing lazy loading,
4:10 or joins or any of those kinds of things, because it's a sweet document database.
4:15 Notice, also that some of these over here, some of these have a service history,
4:22 some of them don't even have engine,
4:24 this one doesn't have an engine or a service history,
4:26 and that's probably ok, right, the schema is flexible in MongoDB,
4:29 the schema is not flexible in our application, it's a class,
4:33 it is what the shape of that class is, period,
4:35 but in the sort of evolution of this as the data grows,
4:39 you can think of this as like an older version of this record
4:42 and here is a much newer one, it has an engine and a service history, right,
4:46 but if we ask questions about show me the cars
4:49 with the service history greater than this, you know, five or greater,
4:52 these are not going to come up, they will just not appear in those results.
4:56 So it's probably okay if we really need to upgrade them,
4:59 to make sure the shape is exactly matching
5:02 we could just run a script, the script would be a Javascript migrate script
5:04 rather than a sequel dvl type migrate script, but a script nonetheless.