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


Talk Python's Mastodon Michael Kennedy's Mastodon