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