Communicating with a MongoDB database using Motor
As we mentioned at the beginning of this chapter, working with a document-oriented database, such as MongoDB, is quite different from a relational database. First and foremost, you don’t need to configure a schema upfront: it follows the structure of the data that you insert into it. In the case of FastAPI, it makes our life slightly easier since we only have to work with Pydantic models. However, there are some subtleties around the document identifiers that we need to take into account. We’ll review this next.
To begin, we’ll install Motor, which is a library that is used to communicate asynchronously with MongoDB and is officially supported by the MongoDB organization. Run the following command:
(venv) $ pip install motor
Once you’ve done this, we can start working!
Creating models that are compatible with MongoDB ID
As we mentioned in the introduction to this section, there are some difficulties...