MongoDB with Async Python Transcripts
Chapter: Welcome to the Course
Lecture: What is Beanie and What is an ODM?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Speaking of Beanie, we're going to be using the Beanie ODM. What is that? Well, you look at their
0:08 doc here, it says Beanie is an asynchronous Python object document mapper for MongoDB,
0:15 and the data models are based on Pydantic. Let's take that apart. Asynchronous, that means we can
0:21 use or I guess really have to use async and await to talk to it, which means Beanie will automatically
0:28 break up interacting with the database into the hotspots where we're actually working with it,
0:33 and allow us to interleave other work. While we're waiting on MongoDB, or on the network,
0:39 or on DNS, or you know, whatever's happening to complete that database query or insert,
0:45 we could be doing other database calls also to that same database server. Or we could just be
0:51 processing more requests, we could be calling API's, who knows what but asynchronous like we
0:56 just talked about, it's going to allow us to do that. Object document mapper, ODM, you're
1:01 probably familiar with the term ORM, but document databases, of which MongoDB is one, generally
1:09 don't have relationships. There are ways to model relationships, but they're strongly
1:13 de-emphasized. So what we get out of MongoDB are documents, not records, and so we have
1:21 an object document mapper just a little bit more accurate acronym. But think ORM, but
1:27 for document databases. And finally, Pydantic. Pydantic is an incredible way to model classes.
1:34 It supports serialization from JSON, it supports transformation and validation. It's type
1:40 safe, really, really cool technology. And if you're using things like FastAPI or other
1:46 tools that use pydantic? Well, guess what, it's really awesome that your database model,
1:52 and maybe your API model can be compatible, or maybe even the same some of the time. So
1:57 beanie, a singers Python ODM, or MongoDB based on pydantic. Really awesome. Now before we
2:06 dive into learning about MongoDB and some of the asyncio stuff and pydantic foundations,
2:13 I want to give you a quick, quick glance, a little bit of a preview of what it's like
2:17 to create a class that matches a MongoDB document and how we query that with Beanie and async and await.
2:26 So in this example here, we've got some code, we create an embedded part of our document called category that has a name and description.
2:34 And then we have a product that has also a name and description a price, this one is indexed. And here you can see the embedded categories there.
2:42 Also in the class, we have a inner class called settings that allows us to configure how the ODM match it maps over to MongoDB.
2:51 For example, what is the name of the collection, what indexes are there, and so on. Pretty clean way to create classes, right?
2:58 And once you have those, how do you query them? Well, there's a bunch of cool examples on the documentation.
3:03 But let's just see how we might go to our product database and find all the ones that are in the category chocolate.
3:10 There's a few interesting aspects here. Notice it says chocolates equals await product dot find.
3:16 So instead of just saying product dot find and blocking, we say await that allows us to break our code into these smaller tasks that we saw earlier.
3:27 So that's the only difference to do the async stuff, but it's awesome, right?
3:31 And then notice our product class had the categories field in the categories had a name and a description.
3:39 So we can automatically traverse that hierarchy within the document by just saying product
3:44 dot category dot name, equal equal, the value we're trying to search for, as well as the price being less than $5.
3:52 So this query is going to give us all the chocolate items that cost less than five units, I'm guessing dollars, euros, something like that.
4:00 And then we can call to list to serialize that in memory into a list.
4:06 Alternatively, it could have been a cursor that we could work across, but to list is good if you want to have it all at once. All right.
4:14 So really cool way to create classes that model documents, including the nested ones
4:19 and a really slick way to query them, both from a how do I talk about the query itself as well as the async and await.


Talk Python's Mastodon Michael Kennedy's Mastodon