MongoDB
Setup mongo Database standalone
docker pull mongo
docker run -p 27017:27017 --name my-mongodb -v my-mongodb-data:/data/db -d mongo
For Mongo DB, download MongoDB Compass for graphical UI for interacting with mongoDB:
Python Mongo DB Drivers:
There are two popular drivers available:
- PyMongo (Official)
- MongoEngine (Wrapper over PyMongo, provides Object Mapping similar to SQLAlchemy)
PyMongo
Repository: https://github.com/mongodb/mongo-python-driver
Examples:
Mongo Engine
A Python Object-Document-Mapper for working with MongoDB
Repository: https://github.com/MongoEngine/mongoengine
Examples:
Comparison
PyMongo vs Mongo Engine Comparison
Python FastAPI with pymongo
If you are using Python FastAPI, you can go with pymongo as there are already models defined for FastAPI.
Pymongo Tutorial with FastAPI | Github Repo
Python Unit Tests and Mocking Options
- https://github.com/schireson/pytest-mock-resources
- https://github.com/mdomke/pytest-mongodb
- https://github.com/mongomock/mongomock
Concepts
Replication
Replication means you have several nodes. You have one PRIMARY and one or several SECONDARY. As term "repliaction" implies the SECONDARY have an exact copy of the PRIMARY. An application can write only to the PRIMARY, however it can read either from PRIMARY or SECONDARY.
Sharding
Sharding means you distribute the data over several nodes. In your case you put even ID's to one node and odd ID's to the other node. This is very similar to MongoDB sharding, however typically you just define the sharding key and MongoDB takes care how to distribute the data evenly.