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