Aller au contenu principal

MongoDB Database Configuration

Overview

The Movie List Application uses MongoDB as its primary database to store movie information and user reviews. MongoDB is a NoSQL document database that stores data in flexible, JSON-like documents called BSON (Binary JSON).

Database Setup

Container Configuration

The MongoDB database runs in a Docker container named mongo-local using the official MongoDB image:

mongo-local:
image: mongo:latest
container_name: mongo-local
ports:
- "27017:27017"
volumes:
- ./mongo-init:/docker-entrypoint-initdb.d

Key Configuration Details:

  • Image: mongo:latest - Uses the most recent stable MongoDB version
  • Port: 27017 (MongoDB's default port) is exposed to the host
  • Initialization: The mongo-init folder is mounted to /docker-entrypoint-initdb.d

Automatic Database Initialization

MongoDB automatically executes any scripts placed in /docker-entrypoint-initdb.d when the container starts for the first time. This project uses this feature to populate the database with initial movie data.

Database Structure

Database Name

  • Database: movie-api-db
  • Collection: movies (stores all movie documents)

Connection Configuration

The Spring Boot application connects to MongoDB using different configurations based on the environment:

Local Development (application-local.properties)

spring.data.mongodb.host=mongo-local
spring.data.mongodb.port=27017
spring.data.mongodb.database=movie-api-db

Explanation:

  • host=mongo-local: Connects to the Docker container named "mongo-local"
  • port=27017: Uses MongoDB's standard port
  • database=movie-api-db: Specifies the database name

Production (application-prod.properties)

# Commented out - would use MongoDB Atlas or other cloud service
# spring.data.mongodb.uri=mongodb+srv://user:password@cluster

Data Initialization Process

1. Initialization Script (init-mongo.js)

const fs = require('fs');
const path = require('path');

// Read the movie data from JSON file
const dataPath = path.join('/docker-entrypoint-initdb.d', 'data.json');
const data = JSON.parse(fs.readFileSync(dataPath, 'utf8'));

// Connect to the database
const db = connect('mongodb://localhost:27017/movie-api-db');

// Insert all movie data into the movies collection
db.movies.insertMany(data);

How it works:

  1. File Reading: Reads movie data from data.json file
  2. Database Connection: Connects to the movie-api-db database
  3. Data Insertion: Uses insertMany() to add all movies to the movies collection

2. Movie Data Structure (data.json)

Each movie document contains the following fields:

{
"imdbId": "tt3915174",
"title": "Puss in Boots: The Last Wish",
"releaseDate": "2022-12-21",
"trailerLink": "https://www.youtube.com/watch?v=tHb7WlgyaUc",
"genres": ["Animation", "Action", "Adventure", "Comedy", "Family"],
"poster": "https://image.tmdb.org/t/p/w500/1NqwE6LP9IEdOZ57NCT51ftHtWT.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/r9PkFnRUIthgBp2JZZzD380MWZy.jpg",
// ... more backdrop URLs
]
}

Field Descriptions:

  • imdbId: Unique identifier from IMDb (Internet Movie Database)
  • title: Movie title
  • releaseDate: When the movie was released (YYYY-MM-DD format)
  • trailerLink: YouTube URL for the movie trailer
  • genres: Array of genre categories
  • poster: URL to the movie poster image
  • backdrops: Array of background image URLs for the movie

Data Persistence

Development Mode

By default, data is not persisted between container restarts. When you stop and restart the containers, the database is reinitialized with the original data.

Enabling Persistence

To persist data between restarts, uncomment the volume configuration in docker-compose.yml:

volumes:
- mongo-local-data:/data/db

This creates a named Docker volume that survives container restarts.

Database Operations

The Spring Boot application interacts with MongoDB through:

  • Spring Data MongoDB: Provides repository pattern for database operations
  • Automatic Mapping: Converts between Java objects and MongoDB documents
  • Query Methods: Allows finding movies by various criteria (title, genre, etc.)

Benefits of This Setup

  1. Quick Development: Database is ready with sample data immediately
  2. Consistency: Every developer gets the same initial dataset
  3. Flexibility: NoSQL structure allows easy schema changes
  4. Scalability: MongoDB can handle large amounts of movie and review data
  5. Docker Integration: Database runs consistently across different environments