MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Concept: Registering connections

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the very first things if not the first thing
0:03 that we need to do is register our connections.
0:05 So it's really straightforward, we just import MongoEngine
0:08 and then we call register connection,
0:11 and you want to give this connection an alias or set it as the default,
0:14 and then we're going to set the name equal to the name of the database.
0:18 Here we're calling this one core, I like to be very explicit and say
0:22 everywhere that you are working with a connection or a database really
0:26 you name that explicitly in your code
0:29 as we'll see later when we get to the classes.
0:31 So we register connection, and we set the alias to core,
0:35 and the db we're going to say name = dealership.
0:38 Now, this worked well if you're just connecting on local host
0:41 no authentication, default port all that, right
0:44 we just let everything else fall through the cracks.
0:46 When we get to the production deployment,
0:49 well that's not really going to fly anymore, we're going to need ssl,
0:52 we're going to need to enable authentication
0:54 and pass credentials and all that kind of stuff,
0:56 so we can use a more complicated variation here,
1:00 where we do basically the same thing, but we create this dictionary
1:03 that has the additional elements, now it doesn't have to be a separate dictionary
1:07 you could just set them explicitly, but it turns out that sometimes
1:10 if you want to like put this into your log or things like this, it's kind of handy,
1:14 so we're going to basically set a username, password, host, port,
1:17 authentication source is almost always admin, not always,
1:21 it's either the database you're working with if it's a local user
1:24 or if it's a server wide user you're using to be on admin
1:27 authentication mechanism is scram-shah-1,
1:30 or you can change it that's the default and ssl is true,
1:33 and in this case, we might be using a self signed certificate
1:37 which is totally good for the encryption, but it's going to fail
1:40 and say we don't trust the certificate, if we trust the server
1:43 you can go with ssl cert requires none,
1:47 or if you want to make sure you have one, trust its certificate, omit the last line.
1:50 And then we just use **data basically to pass those
1:53 as keyword arguments to register connection
1:56 and notice, each step I'm saying get the user from config or environment
2:00 so this could be in a web app where these values are stored in the config file,
2:04 you don't want to put them in source specifically
2:07 you don't want them checked in anywhere ever,
2:09 you could say get them from, you can put them in environment variables
2:12 on your server and then grab them at runtime
2:14 out of the environment and set them here.