Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Connecting to MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, let's get started with MongoDB. We're going to do something very similar to what we did with our relational database
0:07 and we're not going to write in raw SQL. When we did our relational stuff we worked with SQLAlchemy and classes.
0:14 Same thing here, we're not going to write in the lowest level query syntax and just exchange dictionaries with MongoDB
0:21 which is the lowest level, by the way. We're going to use something that is sort of a parallel or the equivalent of SQLAlchemy
0:27 something called MongoEngine. So, let's start by adding that as a requirement here. Notice now, we're running 3.6.
0:36 You may not have noticed before we were running 3.7. At the time of this recording 3.7 just came out and MongoEngine does not support it.
0:45 There's some incompatibility. They have a fix in GitHub. It's not yet published to PyPI. I'm going to go ahead and install that.
0:56 That's good, and apparently, I have an old pip. So, this is the old pip in my new virtual environment, right? We have MongoEngine satisfied.
1:06 Great, that was super easy. That was the same as putting SQLAlchemy here. The other thing we did with SQLAlchemy remember, we created this DB session
1:15 with the connection string and then we created the engine with the connection string and then we created the session factory from that
1:23 and all those things? Well, we're going to do all of this but we're going to do it a lot simpler for MongoDB because it is simpler.
1:29 Now, I may well just put it into data. If I was doing a fresh, from scratch only MongoDB site here I would just make a data folder
1:38 and put stuff in there that is MongoDB-related not SQLAlchemy-related. But one of the steps I want to do for you
1:44 in this chapter is to convert, to import all of our data from a relational database into MongoDB. For that to work easily
1:52 we're going to still want these to be around because we need them to do that transformation. So, I'm going to create a second folder
1:58 and we'll call this nosql. But really, data would be appropriate if it weren't already taken. So just like we have this DBSession.factory
2:06 I'm going to create a new file called mongo_setup. And we'll define a function, just like we said before
2:13 global_init, and it'll have a db_name, which is str and it's going to do some really, really simple stuff for a simple case.
2:23 And then, I'll show you a slightly more advanced version that you will have to use in production with all the various settings you have to pass.
2:29 So the simple version is we're going to import mongoengine. I'm going to come down here and say mongoengine.register_connection.
2:36 I'm going to pass an alias. So this is like the shared database connection name. We'll reference this later.
2:43 And then the name, it's going to be the name of the DB so we'll say pypi_nosql. We'll just call it that. That seems decent.
2:52 And that's all we have to set. The host, the port, all those things are going to default across. Though I guess if we're passing the name
2:59 let's say DB name here we can even just default it. So we call it like this or we can override it. Okay, great, this is the simple version
3:08 and this is all we're going to need but let me just drop the real, quote, real version that you'll need for a real complicated remote server
3:18 with authentication and encryption and all that. So instead of this nice simple version which is our localhost version this'll totally work
3:24 we're going to have a slightly more complicated version with our default name there. So here's the version what you just saw
3:32 but here's the version that we're going to use for actually connecting to MongoDB in production with SSL
3:38 and authentication, and usernames, and passwords and remote servers, and all that stuff. Okay, so this is what we need to get connected.
3:45 I'm going to just put that away. Let's go to our init_db. We're going to not do that stuff anymore, are we? We're now going to go mongo_setup
3:55 and we got to import that of course. I think that that needs to be package sub-package so let's do this. That was it. So now we're going to import that
4:09 and we'll say global_init and we won't pass anything just going to do the defaults no user, no password, localhost everything, just like that.
4:17 And this is it. This is enough for us to talk to MongoDB and it also will do all that create table equivalent stuff.
4:24 Okay, so this step is going to get us connected.

Talk Python's Mastodon Michael Kennedy's Mastodon