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