MongoDB with Async Python Transcripts
Chapter: Performance Tuning
Lecture: Uniqueness Index for Users

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we move off indexes, there's still one database class to go here, and that's user. Let's go ahead and add them here.
0:10 Remember, indexes never go on the base model. They go on the top-level document.
0:15 And in here, we're going to almost always want to know if we're tracking it, we're tracking
0:21 the created date, we very likely will want to be able to ask questions like, ""Show me the users created today.
0:27 going to be a filter into a before after time window, or we could also be sorting by the most recently created ones.
0:36 So that's going to lead to two indexes, created date and last login, because we probably want
0:43 the newest who is logged in most recently and who has was recently created. Let's put descending as that would be the default way to go.
0:52 The other thing we're going to want to have is find users by their email for sure.
0:58 So that gives us one more index which will be find them by their email, ascending, descending. We're not going to sort by email so it doesn't matter.
1:05 But this is interesting. This is new and we have not seen this part yet. You can specify that this is unique.
1:13 So remember we didn't want to use the email as the primary key as the underscore ID which itself would have been unique.
1:20 That would be not good because we want people to possibly change their email but not change their primary key.
1:27 Still though, we don't want two users with the same email. We kind of talked through that scenario before, but here's how we make it happen in Beanie
1:36 and MongoDB. So if I run this again, refresh our indexes. Now we're looking at users, of course.
1:50 one options that unique, you can only have one email. Perfect. So that's what we want.
1:59 And we can try to verify that this is working. So if we run this, we come over here and say
2:06 we want to create a user, the user is Michael Kennedy, I think I'm already in here. We'll We'll see. Error, cannot create a user with Michael Kennedy
2:18 at talkpython.fm already exists. How do we know that? How did that surface to us? Over here, let's track this down. So we go to create user.
2:31 We're just checking now, but if we actually tried, if we turn this off, okay, I guess if we just turn this off,
2:37 we're being a little bit kind and checking ahead of time. You don't want to ask these questions and then tell them all that data you entered is wrong.
2:47 But let's say we'll create a user, a letter A as you know, Oregon, USA, ernt, exception. We're making our way down.
2:58 Eventually we'll get an exception all the way down. Here we have that MongoDB is not happy with us trying to do the insert.
3:10 That's the one we're looking for, a duplicate key error out of PyMongo.errors. That was caused by this index that we set.
3:19 You can even see the index, email ascending, duplicate key, and on and on it goes.
3:25 All right, so it is impossible for us to insert data that would violate the uniqueness constraint. Let's put these back.
3:36 So it's got a little more friendly interaction, but if you try to force it into the database, you can see it's not going to let it go.
3:43 And the reason for that is over here is this unique equals true. Excellent.


Talk Python's Mastodon Michael Kennedy's Mastodon