Add a section on setting up MongoDB

This commit is contained in:
Peter Vacho 2024-12-30 15:34:03 +01:00
parent be77031ddb
commit 7191e9ab09
Signed by: school
GPG key ID: 8CFC3837052871B4

View file

@ -185,3 +185,61 @@ variable. It works in the following way:
`TRACE_LEVEL_FILTER="src.api.foo.foobar,src.api.bar.barfoo"`
- When `TRACE_LEVEL_FILTER` starts with a `!` symbol, followed by a list of loggers, the root logger will be set to
`TRACE` level, with the specified loggers being set to `DEBUG` level.
### MongoDB
As you probably noticed, the project uses MongoDB as the database. If you're not familiar with MongoDB, it's a NoSQL
database, which is very easy to use and set up. You can find more information about it on the [official MongoDB
website](https://www.mongodb.com/).
To set up a MongoDB instance, you can either use the provided docker-compose file, in which case you don't need to do
anything, or set it up manually. For manual setup, you can follow the [official installation
guide](https://docs.mongodb.com/manual/installation/).
#### Quick MongoDB setup
If you just need a quick MonogDB instance that you can spin up during the development, I'd recommend using Docker. Note
that you don't need to follow the Docker installation for the entire project, e.g. using docker-compose, you can run the
project normally and just host the MongDB instance through Docker. To do this, simply run:
```bash
sudo docker run -d --name mongodb \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=test \
-p 27017:27017 \
mongo:latest
```
This will start a MongoDB instance with the root user having the username `root` and password `test`, using the admin
`authSource`. The instance will be available on port 27017, so you can use it with the following connection string:
```dotenv
MONGODB_URI=mongodb://root:test@localhost:27017/my-cool-database?authSource=admin
```
Once you're done, you can stop the instance by running:
```bash
sudo docker stop mongodb
```
To also remove the container, you can run:
```bash
sudo docker remove mongodb
```
> [!NOTE]
> This MongoDB instance will not persist the data across runs. If you remove the container, all the data will be lost.
> If you need the data to persist, you can use a docker volume or a bind mount, as already explained in the
> [Docker](#docker) section.
#### Quickly populate the database
During the development, it's often useful to have some data in the database to work with. To quickly populate the
database with some, you can use the provided `populate_db.py` script. To run it, make sure you have activated the
virtual environment and then run:
```bash
python populate_db.py
```