Skip to content

AlmonChoi/Python-MongoDB-Tutorial

Repository files navigation

"Python-MongoDB-Tutorial"

PyPI version

Description

Simple Python program support Mongo database creation using direct access or via object Class

Built With

Syntax Description
Python Implementation of the API using FastAPI
MongoDB Data storage of the entities created through the API
Docker Provide the required local environment in a container

Installation

Install Python virtual enviornment and required modules:

apt install -y python3.10-venv
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt     # include PyMongo
deactivate

Run with Python virtual environment

docker-compose up -d
source .venv/bin/activate
python initdb.py                    # create user database
python test.py                      # test database operation
deactivate
docker-compose down

Environment variable or configuration file

export APP_CONFIG_MONGODB_URL="mongodb://username:password@localhost:27017/" 
export APP_CONFIG_MONGODB_URL="mongodb://localhost:27017/" 
export APP_CONFIG_MONGODB_NAME="database"

"APP_CONFIG_MONGODB_URL" : "mongodb://localhost:27017/"
"APP_CONFIG_MONGODB_URL" : "mongodb://username:password@localhost:27017/"
"APP_CONFIG_MONGODB_NAME" : "database"

/ root folder

- initdb.py     : To load initalize data from file ./init_data/data.json
- test.py       : To test the function of Class

./init_data folder

- data.json     : initalize user data to setup the database. 

./config

- setting.json  : configuration file

/lib

- __init__.py   : python module folder
- db.py         : Class Datasource
- user.py       : Class User

About

Simple Python program support Mongo database creation using direct access or via object Class

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published