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