Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: The user entity with mongoengine

Login or purchase this course to watch this video and the rest of the course contents.
0:00 SQLAlchemy and MongoEngine have a lot of similarities
0:02 and that's good because we've already set up
0:04 everything in SQLAlchemy
0:05 and you'll see it's easy to do this as well in MongoEngine.
0:09 So over here in the SQLAlchemy version
0:11 we have a users file, so let's go create one like that here.
0:16 And then in this one we have a class.
0:18 It derives from SQLAlchemyBase
0:20 so the table name has some columns.
0:22 Let's just copy this over here and then work on it.
0:26 So it turns out we're not going to need those
0:28 but we're going to need to import mongoengine.
0:31 Now, how does this change if we move to MongoEngine
0:34 instead of SQLAlchemy?
0:35 Well, the Base class changes.
0:37 We're going to derive from a document.
0:39 If you're doing embedded objects
0:40 embedded within larger documents
0:42 you can do embedded document here
0:43 but we're just doing a document.
0:44 These are the top level ones.
0:47 That part doesn't make sense anymore.
0:48 Now let's see, here we have a auto-incrementing primary key
0:52 and this is basically an auto-generated ID in SQLAlchemy.
0:55 MongoEngine documents already come
0:57 with a primary key that's set up
0:59 that's kind of a good thing.
1:00 It's called an ObjectId.
1:02 So if we just omit that
1:04 it's going to create its equivalent of that.
1:07 Now it gets interesting. Now we have a name
1:08 and this is a column of type string.
1:11 How does this look in MongoEngine?
1:12 We're going to come over here
1:14 and say mongoengine.StringField like so.
1:19 And here it's nullable is True
1:21 so we could say required is False
1:23 but that's the default behavior.
1:24 So only if nullable or False
1:26 will we have to say required is True.
1:27 It's kind of opposite.
1:29 But it turns out that these two are equivalent
1:31 so just drop that line.
1:33 And it's going to be pretty similar here as well.
1:37 So we have our string. Have this.
1:41 I have some other things here nullable as true
1:43 so we can leave that apart. Unique.
1:48 It's True for this one.
1:49 And we're also going to have an index.
1:51 Okay, so we can't put indexes in the columns
1:54 we put them in a different spot
1:55 so I'm just keeping those as a little note right here.
1:57 Let's see about this one.
2:00 All that's good, and I'm just going to remember that this requires an index.
2:03 This one. This is a couple cool interesting features.
2:06 It's a Datetime. It has a default value and so on.
2:09 So let's go over here and say this is
2:10 a mongoEngine.DateTimeField
2:14 and guess what it also has a default just like this.
2:18 And it's also going to have an index
2:20 but not right there.
2:22 These last two we didn't use, so I'm just going to drop them.
2:25 All right, looks like we're off to a pretty good start.
2:28 This is a little bit of wackiness here
2:30 so let's do a little clean up.
2:32 Now, we want to specify the indexes.
2:34 We also want to control the table name
2:36 to be lowercase users.
2:37 So we just do that differently here.
2:39 So we have a meta dictionary.
2:41 And over here instead of having
2:42 a table we have a collection.
2:45 And this is going to be users, like so.
2:49 And we have a db_alias.
2:52 Remember, here where we say core.
2:57 We want to set that here.
2:58 All right, this is how you select
2:59 the connection it's going to use.
3:01 Or even the server, and the database
3:02 all that stuff. Okay, so we've got that.
3:04 And then final we want indexes.
3:08 And this can be a list of either strings or dictionaries
3:11 that contain, like, from composite indexes
3:13 things like that. So we want one for email
3:17 we want one for hashed_password
3:20 and we want one for created_date.
3:23 Okay, then that means we can remove these little notes.
3:25 And this should be entirely done.
3:28 Is it exactly the same as our other user? No.
3:32 These two are definitely different
3:34 but you can see they're strongly similar.
3:37 So is the way in which we query them
3:39 and insert them, and so on.