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