MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Concept: Atomic, in-place updates

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Despite the fact that MongoDB is a NoSQL database it does adhere to the acid properties under certain circumstances.
0:08 Primarily that means updates to individual documents are guaranteed to be atomic, and along with those, we can get great performance
0:16 as well as safety if we don't pull the document back for the database, make changes and push it back hoping no one else has changed it
0:23 during that intervening time there, but in fact we can go to the database and go make this change here
0:28 I don't care if it's a 100k document, don't pull anything back just make this little change and that happens atomically and safely.
0:35 So the operators that we have to work with are increment, multiply, rename a field, set on insert set unset, like basically delete a field,
0:43 min and max so I would like to set the value but only if this value is lower than the one I'm passing,
0:49 or the one that's in the document or set it to the max, like only set the value to this if this new value is bigger than the existing one.
0:56 You can also use current date to basically grab the server date and save it there as well.
1:01 So these are the in place individual updates and we can see how that works so we'll come over here and let's insert just a book
1:07 and this time our book has a view count, right, the view count is zero, maybe every time somebody pulls up the book we want to increment that,
1:14 so we can say test.update and give it the object id right here is a real simple one so it was fits onto the screen basically
1:21 you can say $inc increment view count by one, and we do this a few times, so we've done it three times
1:27 it should go from zero to— well you guessed it, three and it all happened atomically in the database,
1:33 without us ever pulling it back or worrying about any sort of concurrency whatsoever. So this is great for working with individual fields
1:40 sometimes we need to work with arrays, so we saw like for example our ratings object maybe we want to work with that atomically.
1:48 So MongoDB has operators for that as well, so we have things like add to set, so suppose it's got like a votes list,
1:56 people who have voted on this book, not the values just keep it simple, just the users who have voted
2:01 and that contains user id, so you could say add to set user id when they vote
2:05 and that would actually only add them there, if they're not already in that list; what's cool about that is
2:12 if they push the little vote button twice, it doesn't count twice, just either you add it there and the person has now voted for or they haven't.
2:18 Another good example is tags, like think stack overflow, I want to tag a post so you could say add the tag Python, add the tag mongo,
2:25 and if it's already there, it's just going to leave it alone if it's new, if it's not there it will actually add the tag.
2:30 So these are really cool to add to set for kind of uniqueness on these subarrays. We also have pop and pull for pulling things out,
2:36 pull all say I want to remove all the votes by a particular user, things like that. Also push, so push is like add the set
2:43 without the unique desk constraint, and that's it, I definitely recommend you think about these atomic updates,
2:48 they are not simple, but they are better performing and they are definitely safer as well.
2:55 Like I said before, it's great that the odm, the object document mapper
3:00 that we're going to look at, MongoEngine automatically does this behind the scenes, we don't ever have to even know how they work,
3:06 but it's important that you know that they exist and why they're good for you when you look at the logs, and you look the performance
3:12 and think about things in that way.


Talk Python's Mastodon Michael Kennedy's Mastodon