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