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'll 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. You can run the following command:
$ pip install motor
Once this is done, we can start working!
Creating models compatible with MongoDB ID
As we mentioned in the introduction to this section, there are some difficulties with the...