liveBook · Manning

MongoDB

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

MongoDB is a document-oriented NoSQL database management system.

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

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works through container setup and a Node.js connection, but defers database design and data engineering.
Examples
Uses a Compose database service, a videos record containing a file path, and a real-database test setup.
What it assumes
Builds on a Docker Compose microservice application while adding the database from a public container image.
What you can do after
Apply it via Docker Compose and the Node.js mongodb driver to store video metadata for a microservice.
How it connects
Connects MongoDB to Docker Compose, microservices, the Node.js mongodb driver, and integration testing.

Also explained in

Compare how 7 books explain MongoDB →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.