MongoDB with Async Python Transcripts
Chapter: Beanie Quickstart
Lecture: Motor, MongoDB's Async Driver
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Hooray, it's time to start working with Beanie. We introduced Beanie before, but recall it's the asynchronous Python ODM for MongoDB
0:10
where the models are based on Pydantic. So that you should already know. One more thing before we dive into the code
0:18
that I wanna talk about is the foundations. So Beanie is built on top of Pydantic, but it is also built on top of something called Motor.
0:28
If you've worked with Python and MongoDB before, the chances are really good that you've heard of something called PyMongo.
0:36
Now PyMongo is MongoDB's official client or what they call a driver for talking to MongoDB from Python applications.
0:44
However, PyMongo is completely centered around a synchronous programming model and has no coroutines, no awaitable methods,
0:54
so you can't really leverage the async nature. you could still use it within Beanie, but there's another driver, another library for Python
1:03
that is 100% async capable, and that's called Motor. So Motor is the asynchronous Python driver for MongoDB, where PyMongo is the synchronous one.
1:15
Now, we will be working directly with Motor here, and you could write whole applications with nothing else but Motor.
1:21
However, you don't get Pydantic, you don't get classes, you just get dictionary exchange.
1:26
So we're just going to be using motor to connect and initialize and basically provide the connection
1:31
to Beanie over to our database and then we're going to hand it off.
1:35
But I want to make sure that you understand that there's two different drivers officially from MongoDB for talking with Python to MongoDB.
1:43
PyMongo synchronous motor async. That's the one we want in this situation.