MongoDB with Async Python Transcripts
Chapter: Beanie Quickstart
Lecture: Lightning Review of Beanie

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's do a lightning review of everything we've covered in that sequence of demos. One of the first things we did was connect to the database.
0:09 And remember, everything you do with regard to talking to MongoDB through Beanie has to be async. So that has to happen in an async method
0:20 and you have to await the function calls there. So in this case, we come up with a database name, we create a async I/O motor client,
0:28 client, that's the MongoDB async driver, and we pass our connection string over.
0:34 And then we just await beanie dot init beanie, and we pass in the database object from motor
0:40 in a list of classes that are supposed to be collections, modeling collections over in that database.
0:48 We saw that if we try to work with something like in this case, the user, and it was not
0:53 associated with a connection yet, beanie will throw an exception and not let us create one. So this has to go first.
1:00 Then we created these model classes. There are Pydantic classes, but more specialized in that, there are Beanie documents.
1:07 And you work with them much like you do Pydantic. We've got a name that's a string. We have an email as a string,
1:15 and we can be more explicit with the Pydantic field types. You can have optional strings for our password. You can have defaults for a created
1:24 or we pass in a default factory. And we can even explicitly set a value.
1:30 Here we're setting image URL to none, even though that's really what it'd be set to anyway.
1:35 In document databases, it's very common to have a hierarchy of embedded objects inside of a larger document.
1:42 And we model that just with base pydantic stuff. So here we have a location, which is not a beanie document, just a pydantic model.
1:51 And because everything is optional this time, when we use it down in the user object, we
1:55 actually create a object that goes there that just has the state and country not set by
2:00 using the default factory and setting it to the constructor or the initializer of the
2:06 location class as we do here. So remember, these are not, not only should they not be,
2:14 it's a really bad idea to make these beanie documents because, you know, take along things
2:19 like the revision ID and the actual ID, none of which makes sense for an embedded object.
2:26 When we insert data, we can create them just like you do something in another class in Python. We await saving them that will insert those two.
2:34 Or if we want to do this as a bulk insert, we can use the type and say user dot insert many and give it a list.
2:41 To query data, we just work with a type again. So user dot find, we pass some constraint.
2:47 Notice how cool it is that we can traverse the hierarchy of the user down into its location, nested objects, and test the country.
2:54 You can sort, and if you want to execute it right away into memory, we call toList, and we have to await that.
3:01 Or we can create a cursor without calling toList, and then asynchronously with an async4
3:08 loop over it one at a time more efficiently if you're not going to store it in memory.


Talk Python's Mastodon Michael Kennedy's Mastodon