This repository contains the code samples for the How to implement data masking in MongoDB tutorial. It covers four data masking techniques:
- view-based masking
- aggregation pipeline masking
- static data masking
- tokenization
The techniques are implemented in Python using a MongoDB Atlas free tier cluster.
Each technique has its own Python file. Before you run any file, replace the <USERNAME>, <PASSWORD>, and <HOST> placeholders in the connection string with your Atlas credentials and cluster hostname.
Run the following command to clone the repository to your machine:
git clone https://github.com/<your-username>/mongodb-masking.gitNavigate into the project folder:
cd mongodb-maskingCreate and activate a virtual environment:
python -m venv venv
source venv/bin/activateOn Windows, activate the virtual environment with:
venv\Scripts\activateInstall the required packages:
python -m pip install pymongo fakerIn each file, replace <USERNAME>, <PASSWORD>, and <HOST> with your Atlas credentials and cluster hostname. You can find your connection string in the Atlas UI under Database > Connect > Drivers.
Each file demonstrates one masking technique. Run them individually with the following commands:
python view_masking.pypython aggregation_masking.pypython static_masking.pypython tokenization.pyRefer to the tutorial for a full explanation of what each script does and when to use each technique.