Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Saving a user

Login or purchase this course to watch this video and the rest of the course contents.
0:00 With our user class defined
0:01 let's see how we might use it.
0:03 Now, we don't really have a whole flow
0:04 put together just yet
0:05 so let's just go cram it into a place
0:07 that it really doesn't belong
0:08 but it will let us see how it works.
0:10 So, after this, we'll be able to create users and save them
0:15 and even be able to create them from the database
0:16 even though none are there yet.
0:18 So how would we go and work with this
0:20 if we wanted to create a new user and save it?
0:22 So, going on, say user, user.
0:24 I'm going to import this temporarily from the right one
0:29 and then, we just set the values, like name is Michael
0:34 email is what my email is. That all looks good.
0:38 We probably should set a password
0:39 when we're ready to do that
0:40 but we'll go through the registration on that side
0:42 and the rest has default values.
0:44 So, once we've created this object, how do we save it?
0:47 Well, SQLAlchemy had this unit of work.
0:49 We go get the unit of work, we create a session
0:51 we add stuff, we do a bunch of work, and we call commit.
0:54 We don't do that in Mongo engine.
0:55 It's active record style
0:57 so each record can just directly be saved, which is simpler.
1:00 Maybe not better, but simpler.
1:02 May or may not be, it's up to you. And this is it.
1:05 That's all we got to do to go to MongoDB
1:08 create the database
1:09 create the collection equivalent of the table
1:12 set the effective implicit schema, enable the indexes
1:15 and then, save this into it.
1:17 All this is basically done just by those four lines.
1:20 So let's try again and see what we get.
1:23 Oh, I guess it restarted itself once and already worked.
1:27 So, this is unique and it's pretty cool.
1:29 We cannot have the email inserted twice
1:32 so let's do, put something here.
1:35 I'll just do a save and the app crashed.
1:37 Okay, that's fine.
1:38 All right, well, let's just go look at the database
1:39 see what we've got. It should be inserted.
1:41 In the second time when we say the bug is true
1:44 Flask restarts this and that's why we're getting the error.
1:46 So, how do we look at MongoDB? There's a bunch of ways.
1:49 I want to use this thing called 3T, Robo 3T.
1:52 This is my favorite way to work with MongoDB.
1:55 Come over here, we're going to talk to localhost
1:57 and notice, we now have a PyPI database
2:00 that has just one collection for the moment.
2:02 We've only defined one entity.
2:04 And here are indexes, like our email is unique
2:08 and things like that.
2:09 If we just go look at the documents
2:11 we should have a couple
2:12 the two that successfully got inserted.
2:14 The first one here and then, this is the second one
2:17 that I made up.
2:18 Again, Flask, when it sees it, a debug flag
2:21 it restarts itself in debug mode
2:23 and so, that's why you got that error both times
2:26 but these are working right?
2:27 These are getting inserted. How cool is that?
2:29 You can even see the default values are working
2:31 as well as the implicit id.
2:34 Now, over here, _id, just say that it gets added
2:38 by the system, but in MongoEngine
2:40 you just say .id. It sorts of omits that _ID.
2:44 Okay, so that's it. We've done a simple insert.
2:47 Now, of course, it doesn't make sense
2:48 to have this here at all, so I'm going to get rid of it.
2:50 We're going to rewrite this system
2:52 so the entire website works with MongoDB
2:55 but there we have it.
2:57 Just make sure our app still runs.
2:58 Hey, look, now it's good. So, that's it.
3:01 You can see it's really, really easy to use MongoDB
3:04 and we want to get going.
3:05 I guess implicit in all of this is I already had MongoDB
3:09 running in the background on my computer.
3:11 Just Google how to install and run MongoDB.
3:14 It's pretty straightforward
3:15 but there're a few steps you got to go through
3:17 and it's not too bad.
3:18 And just get the opensource community edition
3:20 run it, and then, that's it.
3:22 You can go and run this code as well.