MongoDB with Async Python Transcripts
Chapter: Performance Tuning
Lecture: Creating Indexes in the Shell

Login or purchase this course to watch this video and the rest of the course contents.
0:00 If we want to create an index, there's two ways to do this. They could be combined together or they could be two separate things,
0:07 however you want to look at it. First of all, you can actually go to the database directly and create an index.
0:15 Just using the MongoDB shell, you could do this within 3T or you could do this within the MongoSH, the MongoSH, or however you talk to MongoDB.
0:27 And in this scenario, we go and say DB, remember, use whatever database. So in this case, it would be use pypi, then db.packages.createIndex.
0:38 The first thing we pass is the key or set of keys. So in this case, we're gonna say package.releases.majorVersion.
0:47 So we're gonna traverse way down into that hierarchy, through that hierarchy. This is why document databases still work so well,
0:56 is you can have that hierarchy, but you can still do high performance index based queries deep down into those things. Remember, releases is a list,
1:06 and inside that list is a bunch of embedded release objects. Excellent. And then we can set a direction. So either one or minus one.
1:17 This is really used for sorting. So it's not super important just in terms of query speed, but if you're gonna do sorting,
1:24 then it will potentially affect that. You can also set whether it runs on the background and it's a good habit to set the name of this index.
1:33 The reason it's good to set the name is we will see when we get to the Beanie side that we can also set a name there. If those names are different,
1:42 but the actual keys are the same, or somehow, basically if the keys are the same, you're gonna get an exception when you try to set that
1:51 and when you try to run it. It says, well, we tried to create this thing called releases major underscore ascending.
1:56 And there's already one called releases major. And if you don't set a name, they arbitrarily pick a name the different frameworks do.
2:04 So setting it allows you to say in both places, like, no, no, no, this is the same thing. Don't worry about it.
2:11 So you can set it here in the MongoDB shell, independent of Beanie or any other way you're talking to MongoDB. Or what I prefer to do for my apps
2:19 is actually set it inside of Beanie.


Talk Python's Mastodon Michael Kennedy's Mastodon