MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Adding service histories with in-place updates
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So we are able to service these cars, however, there is something we might consider, it's probably fine for this type of application,
0:09
but if there is contention around these documents, like multiple things that you're trying to update the same record
0:15
you could run into some trouble here, right, we could have two separate threads, run this part, they could each enter some stuff here,
0:23
there's actually because the input has super long delay here, and then we append it and save, and which every one got saved last
0:28
would replace the other one and just throw that data away. We could add optimistic concurrency by manually implementing it
0:35
and that would solve that problem, but we could actually make this perform better as well as avoid that problem entirely,
0:42
so let's come over here and let's duplicate this, so hit command d in PyCharm,
0:47
control d on the other Oss, so let's come down here and do this entirely different, so we're going to get rid of this part here,
1:00
and instead of saying this, since we're not pulling back the car, let's actually drop this bit here, so you can compare them directly,
1:07
so we're going to ask for the vin, we're going to create the service history and now, instead of pulling the record back,
1:15
making a change and pushing the entire document which could be like 200 K, we just want to move this data over,
1:22
remember that in the raw api, we talked about the operators, we had $addToSet, and $push, so we want to use those operators
1:36
and just say here, this service record put it onto the list that is on that document under service history;
1:42
so that's what we're going to do now instead, so we're going to go car and I need to find an individual car,
1:48
so I am going to do a query here, so I'll say vi number = vin, right, just like we did in our find up here, but instead of pulling it back
1:58
we're going to take this and say update one, when we update one, I want to say something like say service history
2:10
and we'll say service, but how do I tell it to use the operator, this is the first time we've seen this but this,
2:15
but this pattern you'll see recur over and over and over in MongoEngine, how do I tell it to use the operator?
2:20
You'll see that there's a couple of times, a couple different situations where MongoEngine uses double underscores
2:26
to like represent, not just the value but the value with an operator so the first one that are going to see is this push,
2:34
we'll say push double underscore, and what that means is we're going to push service onto service history.
2:45
Up here before we had a way to say if you gave us the wrong vehicle id number we told you no, there's no car with this,
2:52
there's no car with this bad vi vehicle id number; so what do we do here— well, this doesn't pull it back
3:01
it just updates it if it finds it, so how is this going to work? We need to put our test over here and say if it wasn't updated
3:09
so updated is if it comes back with one, but if it comes back with zero, that is a problem, the exact same problem
3:15
we couldn't find the car with the vehicle id number; so now we don't need to append this and push the whole document back
3:21
it's all in one shot atomically on the server. Beautiful, so this is a much higher performance and safer thing to do
3:28
if we don't want any details about the car, we literally just know that this service goes on that service history for that thing,
3:34
let's try again and see if it works. So first lease the cars, I'll grab the id of the Testarossa,
3:41
and let's try to service it— the price is one two three, the type of service is waxing, so we decided to wash it for them,
3:50
our customer thought the glean on that car was like nobody's business, very happy, there's the moment of truth, ready— boom, car with id not found,
3:59
oh, it totally worked, it totally worked, I just have the wrong error message, wrong case here, let's do a listing and run it again do a list,
4:13
now we have our waxing, what did I do wrong— there's a lot of ways we could check it, but if updated is exactly the one we don't want,
4:21
I could say if not updated or if updated equal zero or if updated less than or equal to zero, things like this, let's just go with == zero
4:31
alright, let's try it again, so we'll list our cars, notice I still have this, so we'll go on service it,
4:37
I'll say I want to update this, the price is twelve dollars, this is going to be just the check up and the customer thought
4:45
something was wrong, we couldn't find it, so they give us a three. So, see, did it work, notice there were no errors
4:51
and now the checkup twelve dollars, also notice the order here is literally the order that we're putting onto that list,
4:58
so very very good, we'll do one more, I guess I'll service something bad
5:07
car with bad then not found, all right now, sorry about that, we got this working. So we used the push underscore underscore
5:16
now the other one, remember I told you about Pythonic versus non Pythonic you could use add to set, right if you want to do sort of a uniqueness thing
5:26
the service history is because of the date field and I'm not even ensure about the embedded objects but certainly, the date field is always different,
5:34
so there's no point in this, just do a push, but if you are pushing like tags or customer ids or something like that
5:39
which could easily be determined if there is a duplicate then you could use that ad to set, but remember in Javascript
5:45
it was add to set like this, here it's the Pythonic variant as it probably should be. Okay, so depending on how you're working with your objects
5:56
this might make sense or this might make sense, I guess I would tend to say prefer this one, this is safer and faster,
6:04
so if you have no real reason to choose one or the other, why not safer why not faster.