MongoDB for Developers with Python Transcripts
Chapter: Working with MongoDB directly from Python: PyMongo
Lecture: Introduction to PyMongo

Login or purchase this course to watch this video and the rest of the course contents.
0:01 All right, the moment you've probably been waiting for is finally here,
0:04 we're going to start moving away from Javascript
0:06 and doing Python for the rest of this course to talk to MongoDB.
0:09 That doesn't mean we might not use the Javascript API in the shell,
0:12 just a little bit more, but for the most part
0:14 we're going to focus now on writing applications
0:17 that talk to and work with MongoDB.
0:19 So we're going to look at in MongoDB's nomenclature
0:23 something called a driver, so a driver is the underlying library or framework
0:27 that you used to talk between your application and MongoDB.
0:30 So here we've got our web app
0:33 and it's going to be using the database MongoDB here.
0:35 A request is going to come in, into our web app
0:38 and it's going to use a particular package, right,
0:40 this is not built into Python, this is something we have to go out and get.
0:43 So the package that we're going to work with
0:45 is built and maintained by MongoDB themselves, and is called PyMongo.
0:50 So this is the core, lowest level access to the database server
0:54 and it does the tone of things for us,
0:56 in fact if you look at many of the odms the object document mappers
1:00 the equivalent of the NoSql orm, they build upon PyMongo, right
1:04 so PyMongo is almost always involved
1:06 when you're talking to MongoDB from Python.
1:09 And it does many things for us, it connects to the database
1:12 whether it's local, remote, over ssl, with authentication, with certificates,
1:16 all that kind of stuff, it actually manages replica sets
1:20 so it knows how to find all the different servers participating in a replica
1:24 and do the fail over if one fails,
1:27 it knows how to go over to the other one, things like that;
1:30 it also knows how to deal with sharding,
1:32 so maybe you have a cluster of ten MongoDB servers
1:36 that are all managing part of the data
1:38 and then participate as a group in the queries, PyMongo does that for us,
1:41 this is generally where you do the crud operations,
1:44 the find, insert, update, delete, and those kinds of things;
1:47 you do the other admin stuff as well,
1:49 like drop tables or create indexes and so on,
1:53 and it even does connection pulling,
1:55 so really this does all the stuff that you need to talk to MongoDB
1:58 and the api is very, very, very similar to what we saw with the Javascript API
2:03 which is why I didn't skim over it, I wanted to say, okay,
2:06 you really learned the Javascript api,
2:08 now you basically also know the PyMongo api,
2:11 findOne with a capital O, no spaces,
2:13 is now find_one, with a lower case o, for example,
2:17 there's a few variations for like say pythonic naming
2:20 but other than that, PyMongo is going to sound
2:22 and feel very, very familiar to you at this point.
2:26 Like many things from MongoDB, PyMongo is open source
2:29 so you can come over here to github.com/mongodb/mongo-python-driver,
2:35 and that is PyMongo.
2:37 So you'll see that you can go look around,
2:40 you can see it's under active development and things like that,
2:43 a lot of stars, so this is like I said, the official driver
2:45 but you also have access to the source, right here.
2:48 So now that we know about PyMongo,
2:50 I hope you're ready to go write some code.