MongoDB with Async Python Transcripts
Chapter: Beanie Quickstart
Lecture: Beanie Quickstart: part 5 settings

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, final thing in the quick start here was how quick was this quick start? It's been a while
0:06 had we've been working this for a little while. So maybe it wasn't that quick, but not a true deep
0:10 dive. There's plenty more to come. Anyway, the final thing in this semi quick start is controlling
0:19 what the database looks like. So if we go over here, you notice that when we are doing our
0:23 queries we have capital U user. Well, this just strikes me as a little bit off a little bit weird.
0:31 The collection by by the very name of it, it collects things. It's not a, it's not a single
0:39 thing. It's a collection of things. And so there should at minimum be users. And because MongoDB is
0:46 case sensitive, I'm not a huge fan of uppercase names for our user here. So I'd rather have this
0:53 called like lowercase users, you know, like this, but obviously, that's not going to return
1:00 any results because there's no collection named users. So how do we control that in
1:05 this place where we just have these classes, right? I don't want to change this to lowercase
1:11 users, that would be equally weird, maybe even worse. So what beanie does is it lets says create a class, an inner class, no base class or anything.
1:23 And the fields of this thing allow you to customize how this looks over in MongoDB.
1:29 So the first one you probably almost always want to set is the name. And this is the name of the collection in MongoDB.
1:36 It can be closely related, like we have users and we have user and users, or it could be accounts, it could be something completely different, right?
1:44 So we're going to call ours users and let's just rerun this and see what happens. It helps if you rerun the new one. There you go.
1:54 See, notice it's actually inserting all the objects because, well, they weren't there before. Let's go and see what happened there.
2:02 If we refresh this, there's now an old users and a new users. There's ways in which you could rename this and stuff, but I'm just going to drop that
2:13 collection and put it back in the way I would like to see it. So now if we run this again,
2:20 look at that, our users are back. Well, they're recreated rather. So this is really cool.
2:26 Let's do one more thing. We did that query up here where we're asking for user.location.country is the USA. Let's run that query over in Mongo.
2:41 Remember, the second thing that goes here is the projection. So let's just say name colon one and location dot country colon one.
2:56 So notice down here, things look simpler. And remember, I don't want the ID. So I'll say underscore ID zero. And bring that up.
3:06 Okay, so here you can see no filters yet. If we put a filter in here, what do we want? We want location dot country equals USA.
3:16 So because that's quoted, I need, or it has been dotted, I need to put those. Let's put this in quotes. And of course, JSON like this.
3:28 Look at this, we're now down to just the three records. How is this query happening?
3:32 We're going to get into this later, but I just want to show you that there's some kind of effect here, .explain.
3:37 If we ask it to explain itself, you can see in the query planner that this is an index filter set, no. And it tells us anything else.
3:49 The winning plan is projection default and then column scan. That's just loop through it. Okay, so what that means is this is not running an index.
3:57 We'll get back to that later. But final thing in this section is, I just want to point out that not only do we have, I'm gonna point out the settings,
4:06 not only controls things like the name, but we can have indexes. And this is probably the time, or the place you spend your most time
4:17 working in this little settings class here. So let's say we're gonna have one for location.country. That's what we were working on before, country.
4:27 If we run this again, go back over here and we can refresh the indexes and look at that,
4:34 we got an index, scooch over, on location.country, ascending, perfect.
4:46 Without the explain, you see we get the same results, but if we run the whole thing, you
4:52 see that the input stage is an index scan using the index location.country for that filter right there. All right, so that's the settings object.
5:02 We're going to spend a lot of time basically once we've modeled our classes to really control
5:08 the MongoDB settings and features of that collection using this settings class.
5:14 Things like indexes, projections, the name of the collection, all of that.
5:21 So really important part, pretty straightforward, but this is where you pass that not Python information but MongoDB information over to the database.


Talk Python's Mastodon Michael Kennedy's Mastodon