MongoDB for Developers with Python Transcripts
Chapter: Working with MongoDB directly from Python: PyMongo
Lecture: Atomic updates

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So back to our example, we've inserted some data and we have this little guard here to say don't insert duplicate data, things like that,
0:08 so let's make some changes to our book here. Let's first of all change the title of the third and fourth book,
0:15 let's just change this mess with this book for example, let's change this to like this, third book like so, all right;
0:21 so we have two ways to do this, one way would be to pull back the entire document, work on it and push it back,
0:28 and this is what I think of as the orm style of working. So we'll say book = db.books.find_one, let's do find_one here
0:37 and we're just going to give it the isbn that we have there. Let's just do a quick little print out of the book
0:47 and just so you understand what we're working with we'll also print out the type, so if we run this,
0:51 we obviously get the book back, super, and you can see it is a dictionary, cool; so, I said I want to change the name here,
0:58 let's actually change something slightly different, so we can work with some more advanced features.
1:03 What I want to do is I want to add the ability to have a user like favorite this book and this might not be a good way to do it,
1:09 I haven't really thought it through because it's just a toy example, but let's suppose we want to have the book store the ids
1:16 of the people who have favorited it, in practice maybe it's better to have the user accounts store the ids of the books
1:21 that they individually favorited, but the mechanics would be identical. So how we're going to do that? Well, to this book, I'm going to add
1:28 something called favorited_by, and this is just going to be an empty list here. Then any time we want to work with it, we can come over here and say
1:42 .append the user 42 did this, and then we can say db.books.update and give it a little query here so we would say the id
1:53 and that's got to be, once we're in Python that's got to be in quotes, say book.get_id, it's going to be the value there
2:03 and then what we're going to put back is just this book, and let's just one more time after this get it back and print out book,
2:09 this should make sure that everything went sort of round trip just fine.
2:13 Ready? All right, look, oh yeah look at that, we got a favorited_by right there, 42. If we run it again, now we won't need to do this,
2:24 we can run it again with 100, now we have two people, two user ids who have favorited this and so on.
2:32 Okay, so this is all pretty well and good, but let's do something better, sometimes it makes sense to go and pull a whole document back,
2:39 look at it, make changes to it and save it. In fact, that's something you'll do quite often,
2:43 but in this case, we just kind of want to say add this little id here to this list called favorited_by and maybe it doesn't even exist.
2:54 So let's do this, let's a copy this again and change this, so now we're not going to use that, we'll use our isbn
3:01 and let's modify book four here, so this does not even have a favorited_by yet. Let's put this in here, so we're going to modify that
3:10 and then let's actually also get it back and print it out at the end; there we go, so we're going to get the book back
3:28 but we're not going to pass the whole book we're going to use one of these in place operators;
3:33 remember add to set, so what we're going to do is we're going to use add to set.
3:37 So in Javascript we could type this really in the shell we can type this $addToSet but obviously, PyCharm is telling us not super good Python
3:45 so what we got to do is put that in quotes, and then the value, we can have actually multiple stuff here,
3:52 so we're going to say favorited_by, and then the thing let's add user id 101,
3:59 now, this seems to be telling me I've got something a little bit off here, yes, so we need that to be the entire update document;
4:07 ok, what we're going to do is we're going to say go find this document, this book with this id, which is notice, it ends in 73,
4:15 this is going to be book four, actually let me comment this out really quick
4:19 and we'll just print out, 73 rather, print out notice there's not even a favorited by yet. So what we're going to do is we want to go add this id here
4:29 so it should actually create this list and then put 101 in it let's see if that's going to work.
4:35 Boom, favorited_by 101, and this time we did not pull it back we used one of our cool operators.
4:42 Now, if this was just push, dollar push is another sort of equivalent, this would have more and more and more 101s,
4:50 but add to set, I should be able to run this code over and over and over and 101 is already there so it's not going in,
4:58 it's better if I say 120, now I run it, now we have those two right, so this add to set is super nice, I don't even need to go to the database and go
5:06 well are they there, no they're not there, ok then I'm going to add them. All right, so I don't even need to do that check,
5:11 I can just use this cool little add to set operator, very very nice. So here's how we use the in place operators,
5:17 there's really not much difference other than we have to put more stuff in strings
5:22 because it's not the shell, it doesn't have like the special understanding of what those mean and even over here,
5:29 it's not Javascript, it's Python dictionaries, which those keys there need to be strings in this case.

Talk Python's Mastodon Michael Kennedy's Mastodon