MongoDB is a NoSQL database used to store structured data that an application updates. For a video-streaming microservice, it holds video metadata—initially the path to each video—so the path no longer has to be hardcoded. Its flexible, schema-free storage is useful when the shape of data may change. section 4.5 section 4.5.1
To add MongoDB to a local microservice application:
- Add a db service using the public mongo:7.0.0 image in Docker Compose. Map host port 4000 to container port 27017; other containers still connect on 27017. section 4.5.2
- Set DBHOST to mongodb://db:27017 and provide DBNAME to the video-streaming service. Install the Node.js mongodb driver, connect with MongoClient, and select the videos collection. section 4.5.2
Listing 4.5 Adding a MongoDB database
version: '3'
services:
db: #1
image: mongo:7.0.0 #2
container_name: db #3
ports:
- "4000:27017" #4
restart: always #5
azure-storage:
image: azure-storage
build:
context: ./azure-storage
dockerfile: Dockerfile
container_name: video-storage
ports:
- "4001:80"
environment:
- PORT=80
- STORAGE_ACCOUNT_NAME=${STORAGE_ACCOUNT_NAME}
- STORAGE_ACCESS_KEY=${STORAGE_ACCESS_KEY}
restart: "no"
video-streaming:
image: video-streaming
build:
context: ./video-streaming
dockerfile: Dockerfile
container_name: video-streaming
ports:
- "4002:3000"
environment:
- PORT=80
- DBHOST=mongodb://db:27017 #6
- DBNAME=video-streaming #7
- VIDEO_STORAGE_HOST=video-storage
- VIDEO_STORAGE_PORT=80
restart: "no"
The Compose listing shows the database service, port mapping, and connection setting together. section 4.5.2
Store a video path
A videos record pairs an _id with a videoPath such as SampleVideo_1280x720_1mb.mp4. This supplies metadata for the streaming service instead of leaving the file path in its code. section 4.5.2 section 4.5
For integration tests, run a real MongoDB instance and load realistic test data rather than replacing the database with a mock. A supplied Docker Compose setup can start that instance. section 9.7.2






