MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Registering connections in mongoengine
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Now let's begin by setting up MongoEngine,
0:03
there's a few start of the app kind of configuration things we need to do
0:06
in order to use MongoEngine, and then we just use
0:10
the classes and types throughout the app.
0:13
So what I want to do is I'm going to create a folder here
0:16
let's call it NoSQL, so we're going to put
0:18
a number of MongoEngine related things in here
0:22
and I don't want to call it MongoEngine because then it will conflict with the name
0:26
so, lacking creativity I'm calling it this,
0:29
now there's a couple of things we need to do
0:30
we need to set up the connections and then we need to define the classes,
0:34
this first part we're just going to set up the connection.
0:39
I'll create a module called Mongo setup, ok
0:43
so down here, let's define a function
0:49
called global init, we are going to call this function from outside.
0:53
Now, in real life later as we talk to like sort of the production stuff
0:57
we're going to want to pass in like the user name, the password, the server name
1:00
all sorts of stuff that you know maybe in a real app comes from
1:04
like a config file or the environment in a production server, something like that,
1:07
but for now we're just going to put this in here.
1:10
So to get started, we have to import MongoEngine,
1:14
we don't need PyMongo but MongoEngine we need.
1:16
And then down here, it's really simple what we need to do,
1:19
we're going to register a connection,
1:22
so we're not actually going to open the connection here,
1:24
this doesn't talk to the database, but it basically says
1:26
look if you have a class that maps
1:30
to a particular type or named part of our application
1:34
use this database connection to do the backend work.
1:37
So we're going to come down and say Mongoengine.register connection
1:40
and see it has alias name and then other,
1:42
and what comes with the other, the .... there
1:46
is like the connection string information
1:48
like server name, port name, host name, use ssl, replica set, all that kind of stuff.
1:52
Okay, so we're going to say, make it really explicit here
1:56
we're going to say alias, I was going to call this core
1:59
and I'll you what that means in a minute, so let's call this demo_dealership.
2:04
Now normally, I would probably just use dealership
2:07
but I already have that for something else
2:09
in a previous example, I kind of want to keep it around
2:12
so we're going to say demo_dealership,
2:16
there we go, and that's all we're going to need to do.
2:18
So the idea is here, we could have multiple things like analytics
2:22
it could be here, and this could be visits or whatever,
2:26
it could be mapping to another database assuming I spelled analytics correctly ;
2:31
so in our classes, we can say this class belongs in the core database,
2:34
whatever that happens to be configured as,
2:37
this one over here, happens to belong in the analytics database
2:40
and so I find it's really valuable if you've got like some core data
2:44
that are required to make your app run,
2:46
and then like huge amounts of extra analytical type data,
2:49
that if you lost, it's like oh well I'd rather have that data
2:52
but if for some reason I want to back up
2:54
let's say you've got 5 GB of analytic data and a 100 MB of core data,
2:59
you could run backups on the core server
3:02
much more frequently than the analytics one
3:04
and by partitioning them to different databases or even different servers
3:07
you can do a lot of cool tricks like that.
3:10
Alright, all that said, we're not doing that,
3:12
we're just going to have one database that we're calling core
3:15
so we're going to register this connection
3:17
and when we get to defining the classes
3:19
you'll see a place where we refer to the core connection,
3:21
that's what we've configured here, and it's going to default local host
3:25
default port everything like that.
3:27
Again, when we get to the using MongoDB in production,
3:30
we're going to talk about how to pass all the extra information you need
3:33
to use this for real, on another server, on another port,
3:36
with authentication, everything,
3:39
but for now, this is what we're going to do to set it up.
3:41
So let's go ahead and get started, using this,
3:43
let's go down here, and we've got our print header,
3:46
let's go ahead and do a config Mongo,
3:59
so it's easy enough to import, let's go up here at the top, our module,
4:09
so we'll just call it Mongo setup like this, and I'll just say global init
4:18
do a little pep8 formatting, and we're good to go,
4:20
and it thinks this is misspelled, no, just short alias for MongoDB.
4:25
Okay let's just run it to make sure everything is working,
4:29
alright, there is no real way to test it yet, but in a moment, we will,
4:32
so far everything worked, we configured our MongoDB connection,
4:35
next up, it's to actually think about modelling these cars
4:38
and owners and service, and all those kinds of things.