Skip to content

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:

  1. PyMongo (Official)
  2. MongoEngine (Wrapper over PyMongo, provides Object Mapping similar to SQLAlchemy)

PyMongo

Repository: https://github.com/mongodb/mongo-python-driver

Examples:

  1. PyMongo Example
  2. PyMongo Example with FastAPI

Mongo Engine

A Python Object-Document-Mapper for working with MongoDB

Repository: https://github.com/MongoEngine/mongoengine

Examples:

  1. Mongo Engine Example

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

  1. https://github.com/schireson/pytest-mock-resources
  2. https://github.com/mdomke/pytest-mongodb
  3. 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.

References

  1. https://stackoverflow.com/questions/42239241/how-to-mock-mongodb-for-python-unittests
  2. https://stackoverflow.com/questions/66223290/mocking-database-calls-in-python-using-pytest-mock