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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So finally we're here in our github repository for our demos, we have something to share, so I have the source folder here
0:08 and let's start with this play around PyMongo. Now, throughout this course, we are going to build what I think
0:13 the pretty comprehensive demo that we're going to work on it for a few hours, it's going to have tons of data, and we're going to consider
0:19 both the design and the performance of the database. But for PyMongo, let's just sort of fool around a little bit here
0:24 and then when we get to MongoEngine, we will take on our proper demo there. So we'll begin by opening this in PyCharm,
0:31 do that little drag and drop trick in MacOS, but on Windows and Linux you've got to say open folder. All right, everything is loaded up,
0:43 and I have created a virtual environment in here a Python 3.6 virtual environment, you can run wherever, but that's the one I'm using;
0:51 now, let's start by adding a file here, so we'll just call this program, we won't do too much structuring and refactoring
0:57 and organizing for this particular demo, we will of course for our proper demo. So, before we can do anything, we just want to type import PyMongo,
1:07 this is not going to turn out well for us, we'll go over here and try to run this, nope, there's no module named PyMongo, so let's go fix that.
1:15 If we all open up the terminal in PyCharm, it's going to automatically find that virtual environment and activate it for us,
1:21 okay, you can see the prompt says .env, that means that we have our virtual environment active, so let's see what is here— not so much, just to be safe
1:33 let's go ahead and upgrade setuptools why are we doing that— because PyMongo actually use a C extensions
1:44 and depending on your system, sometimes setuptools has a little better chance of compiling those, if you have the latest version.
1:50 It doesn't always work that way, and it has a way to fall back to just pure Python
1:55 but the C extensions do make it faster, so that's worth checking out. Alright, so we can pip install PyMongo, now things are looking good,
2:06 let's try a program again, code zero, that means happy, zero is happy. Alright, so we are able to create, or basically import the library,
2:15 now the thing we've got to do is we could just go and create what's called a client and use all the default settings, but in a real app
2:21 you're probably not going to talk to an unauthenticated local database server, you're probably talking to one on another machine,
2:28 maybe there's security, maybe there's ssl, whatever. So let's go ahead and set up the connection string even if you have like sharting, a replication,
2:35 all these things require a connection string. So let's go over here and create a connection string and we'll just put the default values,
2:40 so they always start with the scheme mongodb:// like so, and then local host, and then 270017,
2:48 so this is sort of the default local host sets the default port, it's running locally and the scheme is always here.
2:56 We'll talk about how you can add things like authentication and ssl and what not there.
3:01 So the next thing we need to do is create what's called a mongo client. You can work with connections directly from PyMongo, but you shouldn't—
3:09 why, because PyMongo manages connection pulling for you and reconnect and all these different things, so if you work with a client
3:17 it goes through the connection pulling and that kind of stuff, if you work with the connection directly, you're kind of locking yourself
3:22 into that single connection which is not the best. So we're going to create a pymongo.MongoClient, like this
3:29 I want to give it the connection string like so; now, the way this works, this is basically the equivalent of opening up the shell
3:37 the way it worked in Javascript was, we said use a database, in Python it's a little bit different, in Python we say
3:45 the database is client. make up a database name, literally I could put TheFunBookStore here
3:54 and now this would actually start working with the database called exactly that, we do case sensitivity in MongoDB.
4:01 so let's just call this the_small_bookstore, okay because we're just going to poke around at it
4:07 we're not going to work with that big set of data that we had before yet and we're also not going to work with our main demo.
4:11 So let's call it the_small_bookstore. Now let's go over here and say insert some data it's not fun to have a database with no data, right,
4:23 in fact, let's just really quickly have a glance over here if I connect, notice there is no the_small_bookstore,
4:31 refresh, no, no small bookstore, okay, so this act here almost creates it,
4:37 when you do a modifying statement against this thing you'll see that it does. So let's go over here to books, let's make it a little more explicit,
4:44 I'll say db. so it looks like the Javascript api. So db.books is what we are going to call it,
4:51 we'll say insert and what you want to insert, let's say title, now this is not Javascript, this is not json,
4:57 this is Python dictionaries so you've got to make sure you have the quotes but otherwise it's really really simple.
5:03 The first book, and let's say it has a isbn, let's just put some numbers in there like that and let's do another one, we'll say the second book
5:15 it's going to have an entirely different isbn and while we're at it, let's say go over here and print out the results
5:23 and let's do it again, we'll grab the value and let's print out r.inserted_id, so here let's take a look at the whole thing
5:37 and we'll even print out the type of r, and then the thing that we are usually interested with here is
5:43 when you're doing an insert, remember the _id thing was generated well what was it, what if you want to actually say I inserted it
5:51 and here's the idea of the thing I created for you, somewhere in your app alright, so if we capture the response we can check out the inserted_id
6:00 ok so let's go and run this real quick. Oh whoops, no this is actually just the id, sorry, if you do a bulk answer, I believe you get this
6:10 or you could do, we can come over here and say insert one be a little more focused, now if we insert one we'll have our inserted id,
6:20 let's make this third and the fourth book and make a little change here, there we go, one more time, perfect okay,
6:30 so if you do an insert one we get an inserted one result which is in results insert one result, and here you can see the inserted id
6:38 so we've inserted some stuff, let's go look back at our data base here we should have now, if we refresh it we now have the_small_bookstore,
6:46 if we go to the collections we have our books and we look in the books, that should not be super surprising right,
6:51 those are the things we just inserted, okay so now, let's go over here and do a little test
6:58 we'll say if db.books.count is zero, we'll print inserting data and like this, we'll say else print books already inserted skipping
7:16 and maybe even spell that right huh? Now we run it, nope, there's already books in here
7:24 we're not going to insert duplicate books, so that's all well and good, so we've gone over here and we've connected to the database,
7:32 we've created a client using the connection string and trust me this can get way more complicated
7:38 to handle all the various complications and features of MongoDB, and once we have a client we say the database name
7:44 here I've aliased it to db so it looks like the Javascript api or the shell api you're used to working with, and then we work with the collection
7:52 and we issue commands like find and count and insert, insert one and so on. So now we have some data, let's go maybe do a query against it,
8:00 maybe make some in place updates things like that.

Talk Python's Mastodon Michael Kennedy's Mastodon