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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 It's time to look at the atomic updates. We already talked about the whole document updates and how they work,
0:06 but sometimes it's not really what you want; in the beginning when we talked about NoSQL, we saw that the NoSQL databases gave up things
0:13 that traditional relational databases embraced or considered sacred. One of those where the acid properties, or some part of the acid properties
0:22 and MongoDB does say look things like joints and transactions, transactions mainly being part of the acid properties
0:28 is something that MongoDB doesn't promise so this whole document updates really require an additional layer in the app tier
0:36 called optimistic concurrency, and usually it's fine, sometimes it's not, and you can catch it and say hey look somebody saved this out from under you
0:45 and you do want to keep your changes, their changes, there's things you can do about those types of situations, but not in the database in your app.
0:51 On the other hand, MongoDB does support atomic transactional behavior long as it happens on a single document,
0:58 so if we have a document and let's go ahead and create a whole new collection here called BookReads
1:04 notice it doesn't exist yet, and we're going to insert just an isbn and then how many times it's been read,
1:09 I think of like the Goodreads service or something like that, like I want to know how many of my friends read this book,
1:14 we'll you a simple, simple version of that. So let's go over here and notice we inserted one and if I refresh,
1:19 we should now have that in here in our one record, like so. So we could go and we could do this for this whole document style things,
1:27 I could say book and of course we will be doing this in Python very likely we're just about to leave the Javascript in the dust,
1:37 so let's just print out our book that we got here, notice this has actually given us the same thing back,
1:42 and we could say the read count += 1, we could increment that, and then we could say go over here to the same collection,
1:51 we could say update, I would like to update with this, here's the where clause, and the thing I want to update with is the book,
1:59 so let's say _id : book._id, okay, so this should do that like so, and let's run one more query here at the end to get it back, to see it again.
2:13 Oh yes, find is not going to work, find one however, we don't want to update a whole query, whatever that means it doesn't make any sense
2:24 but let's get one of them back, we know this is really going to be unique and then let's make this change, ok
2:28 so notice, now we've got a read count of one, we do this a few times, bam bam a read count is incrementing over and over and over down here,
2:36 and we're updating one record, so this is cool but this is not part of the acid property guarantees, this could be problematic in lots of ways
2:44 so what we're going to look at now, are the operators that we can use to basically do almost transactional stuff
2:50 and do it in a much more high performance way. So let's go over here again, and let me grab this little clause here,
2:58 all right so we got our document back again and now what we're going to do, is we're going to do our db let me just grab this collection bit,
3:07 and we're going to do our update, in fact update is going to look almost the same, we are going to do this, but instead of passing the whole document
3:17 we're going to pass just an in place atomic operator, all right so what are we going to do, let's suppose we want somebody
3:24 to basically do the same thing, increment that alright, I guess we could just use isbn, that works as well right;
3:38 we're going to need something in our little where clause here, isbn will do. Now by default, this is going to replace whatever's in there,
3:45 that's going to be bad, but what we really want to do is we want to increment that one value, so we can use another operator,
3:51 say inc for increment, and then what do I want to increment, I want to increment let's see what is it called— ReadCount,
4:05 so I want to increment ReadCount by one, I could increment it by negative one, I could increment it by ten.
4:11 So let's run this, now notice we updated one record and let's put this in a way that looks better, nine, ten, eleven, twelve—
4:19 there we go, check that out, isn't that cool? So what's happening here is it's actually going into Mongo,
4:25 go find the document, just change that number right there, just add one to it for me, you don't have to pull the whole thing back,
4:32 make changes and possibly try to put it back and someone else changed it, none of those things, this is entirely atomic and safe
4:39 in a multi threaded, multi server environment, because MongoDB guarantees individual updates to individual documents are atomic
4:48 and because we're not depending on the value, we're not like reading it changing in memory and putting it back
4:54 change it in our programs memory not Mongo's and put it back, then we're not going to have any problems. There's a bunch of cool operators like this
5:01 and we'll see that MongoEngine actually naturally behaves in this style not the document style, even though it's an object document mapper
5:09 which is really really delightful.


Talk Python's Mastodon Michael Kennedy's Mastodon