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