Mongo
This guide shows you how to work with MongoDB locally using Docker Compose, MongoEngine, and Python. This lab demonstrates basic CRUD operations using different execution and validation tools.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in Container- Run the setup script to install tools and dependencies:
Terminal window scripts/setup.sh - Start Infrastructure: Launch the MongoDB container:
Terminal window docker compose up -d
- Run the setup script to install tools and dependencies:
-
Run the Example:
Terminal window python main.py- Enter Shell:
Terminal window scripts/mongosh.sh - Run: Copy and paste the script from
playgrounds/users.mongodb.jsinto the shell.
- Install: MongoDB for VS Code.
- Connect: Connect using the
MONGO_URIdefined in your.env. - Open: Open
playgrounds/users.mongodb.js. - Run: Click the Play icon in the top right of the editor.
- Install: MongoDB Compass.
- Connect: Connect using the
MONGO_URIdefined in your.env. - Navigate: Navigate to
my_db→users. - Run: Open the embedded Mongosh and paste the script from
playgrounds/users.mongodb.js.
- Enter Shell:
How to debug
Section titled “How to debug”-
main.py:
- Open: Open
main.py. - Breakpoints: Set breakpoints in the code.
- Run: Press
F5to start debugging.
- Open: Open
-
Tests:
- Open: Open a test file (e.g.,
tests/test_user.py). - Breakpoints: Set breakpoints in the test code.
- Run: Use the VS Code Testing tab and click the Debug Test icon next to the test you want to debug.
- Open: Open a test file (e.g.,
How to test
Section titled “How to test”- All tests: Run the automated script:
Terminal window scripts/run_tests.sh - Individually: Use the VS Code Testing tab to run or debug specific test cases.
Validate results
Section titled “Validate results”- Enter Shell:
Terminal window scripts/mongosh.sh - Run the following query to see all users:
db.getSiblingDB('my_db').users.find().pretty()
- Install: MongoDB for VS Code.
- Connect: Connect using the
MONGO_URIdefined in your.env. - Verify: Navigate to
my_db→users. - Interactive: Use Playgrounds to run interactive queries.
- Install: MongoDB Compass.
- Connect: Connect using the
MONGO_URIdefined in your.env. - Verify: Navigate to
my_db→users. - Interactive: Use the embedded Mongosh to run interactive queries.
Clean Up
Section titled “Clean Up”docker compose down -v