MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Concept: Updates in mongoengine

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Finally, let's talk about updating documents.
0:03 It's actually really really easy in Mongo Engine to update document,
0:06 once you get one back from the database
0:09 it could be either you've gotten one or you've gotten a whole list
0:12 and you just happen to be making a change to a particular one of them,
0:16 it doesn't really matter, so in this case like see in line three,
0:19 here we're getting a car, we're finding it by id, and we're saying first;
0:22 first of all, we are verifying that we got the car back;
0:25 on line five, we're like no, no car, error
0:28 but let's assume we got the car,
0:30 we're going to create one of these service records
0:32 and we're going to append it to the list that is the service history
0:34 and we want to push that down into the database,
0:37 we want to save that so all we have to do is call car.save
0:39 and it will actually push that down.
0:41 And we saw that there was a possible conflict,
0:44 a possible raise conditions at the database level
0:47 if this type of code or changes to other parts of that car
0:51 that some other operation was being done on the car with the same id
0:54 it's possible that we could overwrite those changes, maybe, not for sure,
0:58 depending on how everybody sort of changed different parts of the car,
1:01 but there could be a problem of saving this here.
1:04 So you want to careful when you're doing this,
1:07 but this works totally fine, most of the time, it depends on your situation
1:11 how actively you're changing, how much contention there is
1:14 for particular documents, but assuming the contention is low
1:17 we're going to be able to say get the car,
1:19 we should make the changes to it and call save,
1:21 and it'll push that right back to the database.
1:23 However, if the contention is high, you care about performance
1:26 or you really just want to take most advantage of MongoDB
1:30 both for safety and performance, you can use the in place updates.
1:34 Here you can see we have this owner object that we've introduced
1:38 and this is like the owner of the car,
1:41 so maybe we want to record how many times
1:43 has this owner been to our service shop,
1:46 owners could own more than one car,
1:48 and so maybe we want to know like for this particular person
1:52 they've been in ten times, even though they have a new car
1:54 that's only been in twice, so we're going to have this number of visits
1:57 which is an integer on the owner
1:59 and we can actually use the increment operator right on it like this
2:03 we can say owner objects id = customer id
2:06 that's like the primary key, then update one,
2:08 increment operator double underscore name of the field
2:10 so incremental_ _number of visits;
2:12 you can increment it by whatever you want
2:14 even a negative number which is really a decrement
2:16 but there's just the increment operator.
2:18 So basically add that number on the right the one here to the number of visits,
2:22 so this is cool for the individual operators
2:25 you can use set, you can use increment, some of those particular operators,
2:28 but if we're going to work with the set in our case
2:32 like we just saw we are adding a service record to a car
2:35 here we could do the same thing,
2:37 but we could do this in place with the push operator
2:39 instead of pulling the document back, adding it and saving it again,
2:42 so we want to create a service record, and this time we're going to say
2:45 card.objects again the queries the where clause if you will it's id is car id
2:52 and then we want to say update one and use the push operator
2:54 so push on to the service history this subdocument.
2:58 In this case, what we get back is the number of updated items
3:00 we set up date one so you can bet it's one or a zero
3:03 and if it's not one something went wrong.
3:06 So it supports in place updates with the individual value operators
3:11 like increment and so on, it also supports things like push and add to set
3:16 for working with sets or arrays in the documents.
3:20 This is both better in terms of safety because you get this transactional behavior
3:24 it's also better in terms of performance,
3:26 because you don't bring the document back
3:28 make changes and push it in, you just shove the delta down to the server.