Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Concepts: MongoEngine and MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's close out this chapter by reviewing some of the concepts that we discussed while going through our conversion from SQLAlchemy to MongoDB.
0:08 First thing, we have to register connections so pretty straightforward, we just pass an alias and a name for the database and off it goes.
0:16 So here we said core and PyPI demo and we call mongoengine.register_connection. As long as our entities use the same alias which is called db_alias
0:26 in their little meta data thing as long as they use the same one it just uses this connection, it's this ambient
0:31 way to get to the database so you don't have to go through that session stuff. The classes that we map to the database
0:38 derive from a built in base class, mongoengine.Document. Everything that's going to be saved to a top level collection is a mongoengine.Document.
0:47 Here we have a package, in this one we're changing what the id is, like we did we used a string instead of an auto incrementing id.
0:55 If we want to take a bson, auto generate a bson id we could just omit that line but this one we want to make sure it's a string
1:02 which is that unique, I guess we don't say unique here we should say unique but that's a feature but it's not one that we set.
1:09 Anyway we want to make sure this is a unique thing and that is just the name of the package and we have datetime fields, string field and so on
1:17 pretty straightforward. What's really powerful though is that we don't have to limit ourselves to just tabular data.
1:24 We can have things like name, summary and homepage that would be tabular but then we can embed stuff. Normally in a relational database
1:31 this takes other tables to manage these many to many relationships. Here we just have maintainers and we're just embedding a list of items.
1:38 If we look at the record in the database it looks like this, we have our id the date, the summary and then we have a bunch
1:46 of maintainers that were put in there. And this is just the id, this is like a relationship back to the user table because it's many to many.
1:52 We're not embedding users in here right but we can avoid that maintainers table that is the normalization table. If we want to do a simple query
2:00 so easy, just package.objects column equals value for direct equal and then you want one you say first, if you want to do all of 'em
2:07 you just iterate over it or you say .all(). Things like that. So we're going to filter on one or more of the fields.
2:14 If you want to filter on more than one and do an and and just comma separate them like any keyword arguments. So in this case the id must match
2:20 the name of the past package name like requests or SQLAlchemy. We call first and this actually triggers the execution of the query.
2:29 If we didn't call that we'd have to loop over it or something, until then we could keep adding to it. And now it's going to return either one or none.
2:37 Let's imagine a world that we haven't been working in yet That's a little bit more complicated. We're doing a hotel or an Airbnb or something
2:45 and we have hotel rooms, those rooms have bookings and the bookings store which guests are in them.
2:52 So we have a booking object, it has an embedded object called guest, that embedded guest object has an id, if we want to answer
3:00 the question of show me all of the bookings for a given user or a given owner we come through and say, go find that owner
3:07 it would do a simple query to get them and then I can go to the room and say go to the room's bookings, this double underscore
3:15 right there says navigate the hierarchy. We have bookings, this is a list of guests or something like that or an embedded singular object
3:24 called guests and it has a guest id. So we're going into the bookings list or embedded item, say go to the guest id
3:32 and then we want to do an inquiry which is a list of ids. So show me the guest, find me all the bookings who have a guest who is contained
3:39 within the owner's family ids. Pretty awesome that we can do this and the way you do it is the double underscores
3:45 both navigate hierarchies and they apply operators like in, so double underscore in that's the operator that's being applied in the query.
3:53 Now one of the patterns that I like to use is make sure there's no data access outside this function. We could return a prepared query
4:03 that's not actually executed and then if we run into an error that error might happen in a view model, even maybe in a template.
4:09 So my best practice for you is to make sure all the data access is done by the time you leave this function, in this case
4:16 a real easy way to do that is convert this to an in-memory list, that means the whole thing is accosted and we're not using
4:22 data access anymore, we're just now working in memory.


Talk Python's Mastodon Michael Kennedy's Mastodon