MongoDB for Developers with Python Transcripts
Chapter: Working with MongoDB directly from Python: PyMongo
Lecture: Concept: Atomic updates
0:01 Let's review the ideas behind these in place updates.
0:03 So here we have more or less a complete MongoDB Python program
0:06 using PyMongo here, so we're going to import PyMongo,
0:09 connect the local database, all the default options,
0:11 and we're going to either create or get access to the bookstore
0:16 by saying client.bookstore, now we're going to insert an object
0:19 that has no favorited by element, right no list, it just has a title and isbn,
0:24 so after the insert, we're going to end up with an _id and a title and an isbn.
0:28 And then maybe we want to add this idea of favorited by,
0:34 maybe you want to design this already that way
0:36 and have an empty list there, but whichever more or less would work the same,
0:41 so we can say I would like to go find the book,
0:43 the first part of our update statement is the where clause,
0:46 so find by primary key and remember, that's when we call insert_one
0:50 that's results.inserted_id, so that's going to find the one and only the item
0:55 and then we're going to use the add to set operator
0:58 and we just pass that as a string in PyMongo,
1:00 and then we'll push on favorited by such and such.
1:03 We could also use $set to set, say $setTitle: the new book with updated title,
1:10 or something like this right, so you can use this all over the place
1:14 and what's really cool, now you may be thinking oh this api is kind of crazy,
1:18 we've got these these dollar operators
1:21 and it's a lot to learn if you're totally new to it, I realize
1:24 but when we get to MongoEngine, you'll see that
1:26 MongoEngine does this transparently under the cover for us,
1:29 so you can actually not have to do this,
1:31 you won't have to necessarily remember all of these
1:34 but you'll get all the benefits that we're describing here.
1:36 If you're using PyMongo, you have to know the api really intimately
1:38 so we're going to push this 1001 user id on to favorited by
1:42 and maybe we'll push 1002 as well
1:46 if people signed up at the same time, they saw the same book, they loved it
1:50 and let's go head and push this 1002 again,
1:52 well not the push operator, but the add to set operator,
1:54 do this again, because it's add to set
1:56 we're going to get a new document that has new book title,
2:00 the same isbn and two items and it's favorited by
2:03 and it's going to be 1001 and 1002, because add to set is item potent
2:07 calling it once or calling it a hundred thousand times,
2:10 it has the same result, other than it might take longer
2:13 to call it a hundred thousand times, right.
2:15 So if it's already there it makes no difference
2:16 but if it's not there to push it in super cool operator,
2:19 really taking advantage of the hierarchical nature of these documents.