MongoDB for Developers with Python Transcripts
Chapter: Course conclusion
Lecture: Lightning review: pymongo
0:06 we're going to write in Python basically for the rest of this course. So the lowest level way to talk to MongoDB from Python is with PyMongo.
0:14 So let's look at a couple of the crud operations here. We'll start of course by importing the package, import PyMongo,
0:21 and if you don't have it just pip install it; and then we need to create a Mongo client by passing a connection string,
0:27 I believe if you actually get a hold of the PyMongo connection you can use it directly, but you should not, because the Mongo client handles
0:35 reconnects and connection pulling is stuff like that whereas the connection itself wouldn't do those kinds of things.
0:40 Then if we want to work with the database, we have this sort of interesting highly dynamic api,
0:47 we go to the client and we just say . (dot) the name of the database so we say client.the_small_bookstore, and we assign that to db
0:55 so it looks like the rest of the shell stuff that we have been doing, but technically that's optional. This database doesn't even have to exist,
1:03 we could create the database in this style just by doing our first insert into it. Whether or not it exists, we get all the database
1:09 and now we can operate on the collections. Let's imagine that in that database there's a collection called books
1:16 and we want to know how many of them are, we would just say db.books.count and that would actually go there and do this operation.
1:23 If it happens to be that either the database of the collection doesn't exist, it doesn't crash, you get zero.
1:40 just be aware that it's not always a one to one exact verbatim match from the native query syntax over to PyMongo. We can also do an actual search,
1:51 before we said find_one I basically got the first here we're going to say I want to find a book by isbn, I want to pass it over,
1:58 here we use Python dictionaries which play the role of those prototypical json objects. We also insert new data, so here we're going to say
2:07 insert this thing which is a dictionary, it has a title called new book and an isbn of whatever is written there and we get back this result,
2:16 the result will have this object id in the field inserted _id, we can go requery it and do all sorts of stuff with it.
2:24 Basically when we say insert one, we get this result which, if it succeeds has the inserted id. Now these are the straightforward crud operations,
2:32 we can also use our fancy in place operators, so here let's just insert this book, so we see what we get, and we grab a hold of the inserted id,
2:39 and now suppose we want to add a field called favorited_by, and this is going to be a list, and we want the list to be basically distinct
2:48 we're adding the ids of the customers or people visiting our site who have favorited in this book, and we'd like to put them in there
2:55 but there's no reason to have them in there twice, that can cause all sorts of problems. We're going to use the dollar add to set, so we run this,
3:02 run it again for 1002, and hey we could run it a second time for 1002, and what we'll end up with is an object that looks like this,
3:09 the two things we inserted, the generated_id and his favorited_by list which has 1001 and 1002. Definitely keep in mind these in place operators
3:20 because they're very powerful and they leverage some of the special properties of the way MongoDB treats documents atomically.