Building Data-Driven Web Apps with Pyramid 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 straight forward. We just pass an alias in a name for the database, and off it goes.
0:16 So here we said core, pypi_demo, and we call MongoEngine.register_connection. As long as our entities use the same alias
0:24 which is called db_alias in their little meta data thing long as they use the same one, it just uses this connection.
0:30 Alright, it's kind of this ambient way to get to the database so you don't have to go through that session stuff.
0:36 The classes that we map to the database derive from a built-in base class mongoengine.Document. Everything that's going to be saved to a
0:44 top level collection is a mongoengine.Document. Here we have a package. In this one, we're changing what the id is, like we did
0:52 we used the string instead of auto-incrementing id. If we want to take a BSON, auto-generated BSON id we can just omit that line
1:00 but this one we want to make sure it's a string which is that unique, I guess we don't say unique here we should say unique but that's a feature
1:08 but it's not one that we set. Anyway, we want to make sure this is a unique thing and that is just the name of the package.
1:15 And we have DatetimeField, StringField and so on. Pretty straight forward. What's really powerful though, is that we don't have to
1:21 limit ourselves to just tabular data. We can have things like name, summary and homepage that would be tabular, but then we can embed stuff.
1:29 Normally in a relational database, this takes other tables to manage these many-to-many relationships. Here we just have maintainers
1:36 and we're just embedding a list of items. If we look at the record in the database it looks like this.
1:42 We have our id, a date, the summary, and then we have a bunch of maintainers that were put in there.
1:47 And this is just the id, this is like a relationship back to the user table, cause it's many-to-many we're not embedding users in here, right
1:55 but we can't avoid that maintainers table that is the normalization table. If you want to do a simple query, so easy.
2:01 Just package.objects(), column equals value for direct equal, and you want one you say first() if you want to do all of them, you just iterate over it
2:09 or you say .all(), things like that. If you want to filter on one or more of the fields if you want to filter on more than one and do an and
2:16 just comma separate them like any keyword arguments. So in this case, the ID must match the name of the past package name. Like Request or SQLAlchemy.
2:25 We call first, and this actually triggers the execution of the query. If we didn't call that, we'd have to loop over it
2:32 or something, until then we could keep adding to it. 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 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 the question of
3:01 show me all of the bookings for a given user or given owner, we'd come through and say go find that owner, going to do a simple query to get them
3:09 and then I can go to the room and say go to the room's bookings, this double underscore right there says, navigate the hierarchy.
3:18 We have bookings, a list of guests or something like that or an embedded singular object called guest and it has a guest id, so we're going into the
3:27 bookings list or embedded item. So we go to the guest id, and then we want to do an in query which is a list of IDs.
3:35 So find me all the bookings who have a guest who is contained within the owners family IDs. Pretty awesome that we can do this.
3:43 And the way you do it, is these double underscores both navigate hierarchies, and they apply operators like in, so __in, that's the operator
3:52 that's being applied in the query. Now one of the patterns that I like to use is make sure there's no data access outside this function.
4:00 We could return kind of a prepared query that's not actually executed, and then if we run into an error that error might happen in a view model
4:08 even maybe in a template. So my sort of best practice for you is to make sure all the data access is done by the time you leave this function.
4:16 In this case a real easy way to do that is convert this to an in-memory list. That means the whole thing is exhausted
4:21 and we're not using data access anymore we're just now working in memory.

Talk Python's Mastodon Michael Kennedy's Mastodon