My solutions for the MongoDB for Java Developers course

Overview

Welcome to M220J

Disclaimer: The dependencies and versions in this project are not maintained. This project is intended for educational purposes and is not intended to be exposed in a network, so use at your own discretion.

In this course we will be exploring how to use MongoDB in the Java environment.

We will be looking into:

  • Different versions and flavors of the MongoDB Java driver
  • How to express CRUD operations in Java code
  • Using the Aggregation Framework to express analytical queries
  • Building an application backend in Java that interacts with MongoDB

Get Started

For this course we will be setting up an application, MFlix, that will allow users to list and interact with a movies catalog (any similarity with other popular video streaming services is pure coincidence!).

In the finished version of the MFlix application, users will be able to:

  • Perform movie searches with the following criteria:
    • cast members
    • genre types
    • text matches in title or description
    • country of origin
  • Create faceted filters on movie searches
  • Create an account and manage their preferences
  • Leave comments on their favorite (or least favorite) movies

... among other features, typical of any movie streaming service.

Your job, as new team member of the team that builds MFlix, and soon-to-be MongoDB Java developer expert, is to implement the backend component, in specific the Database Access Object (DAO) layer. No worries, your team has already built all the integration and unit tests, you just need to get them all to pass!

Project Structure

MFlix is composed of two main components:

  • Frontend: All the UI functionality is already implemented for you, which includes the built-in React application that you do not need to worry about.
  • Backend: Java Spring Boot project that provides the necessary service to the application. The code is managed by a Maven project definition file that you will have to load into your Java IDE.

Most of what you will implement is located in the src/main/java/mflix/api/daos directory, which contains all database interfacing methods.

The unit tests in src/tests/java/mflix/api/daos will test these database access methods directly, without going through the API.

The UI will run these methods as part of the integration tests, and therefore they are required for the full application to be running.

The API layer is fully implemented, as is the UI. By default the application will run on port 5000, but if you need it to run on a port other than 5000, you can edit the index.html file in the build directory to modify the value of window.host. You can find index.html in the src/main/resources/build directory.

We're using Spring Boot for the API. Maven will download this library for you. More on that below.

Database Layer

We will be using MongoDB Atlas, MongoDB's official Database as a Service (DBaaS), so you will not need to manage the database component yourself. However, you will still need to install MongoDB locally to access the command line tools that interact with Atlas, to load data into MongoDB and potentially do some exploration of your database with the shell.

Local Environment Dependencies

There are two main system dependencies in this course:

  1. Java 1.8
  • The java version this course is built against is Java 1.8. You can download the appropriate version for your operating system by clicking here
  1. Maven
  • We use Maven to manage dependencies for the MFlix project. Click here to download Maven

Java Project (MFlix) Installation

The mflix project is supported by a Maven POM file that deals with all the dependencies required, as well as providing the test and run commands to control our project. This means that you can run all the tests and deploy the mflix backend from the command line with Maven.

However, we recommend you use a Java IDE to follow along with the lessons and to accomplish the Tickets assigned to you in the course.

You can use any IDE that you like, as you do not need to have a specific product to complete the course. It would be better if your IDE supports Maven POM files, so it can set the dependencies correctly, otherwise you will need to download and install manually the different libraries and drivers used by the project.

That said, all the lectures and examples of this course have been produced using IntelliJ IDEA CE edition. You will find a lesson dedicated to setting up and exploring this IDE for the course.

Once you downloaded and unzipped the mflix-java.zip file, you will find the project folder. The project folder contains the application code, the pom.xml file that you would import into your IDE, and the dataset required that you will have to import to Atlas.

$ ls
mflix README.rst
$ cd mflix
$ ls
src pom.xml data

Running the Application

In the mflix/src/main/resources directory you can find a file called application.properties.

Open this file and enter your Atlas SRV connection string as directed in the comment. This is the information the driver will use to connect. Make sure not to wrap your Atlas SRV connection between quotes:

spring.mongodb.uri=mongodb+srv://m220student:m220password@<YOUR_CLUSTER_URI>

To run MFlix, run the following command:

cd mflix
mvn spring-boot:run

And then point your browser to http://localhost:5000/.

It is recommended you use an IDE for this course. Ensure you choose an IDE that supports importing a Maven project. We recommend IntelliJ Community but you can use the product of your choice.

The first time running the application might take a little longer due to the initial setup process.

Running the Unit Tests

To run the unit tests for this course, you will use JUnit. Each course lab contains a module of unit tests that you can call individually with a command like the following:

cd mflix
mvn -Dtest=<TestClass> test

For example to run the ConnectionTest test your shell command will be:

cd mflix
mvn -Dtest=ConnectionTest test

Alternatively, if using an IDE, you should be able to run the Unit Tests individually by clicking on a green play button next to them. You will see this demonstrated in the course as we will be using IntelliJ.

Each ticket will contain the command to run that ticket's specific unit tests. When running the Unit Tests or the Application from the shell, make sure that you are in the same directory as the pom.xml file.

Comments
  • Ticket: Principle of Least Privilege

    Ticket: Principle of Least Privilege

    Ticket: Principle of Least Privilege

    Task

    For this ticket, you'll be required to add a new user on your Atlas cluster for the MFlix application to connect with.

    The user should follow credentials:

    • username: mflixAppUser
    • password: mflixAppPwd

    This user should have the readWrite role on the sample_mflix database. Use Add Default Privileges to assign the user this specific role.

    After you have created this user, modify the SRV connection string in your configuration file so the application connects with the new username and password.


    Testing and Running the Application

    There are no unit tests associated with this ticket.

    opened by CSalih 1
  • Ticket: Handling Errors

    Ticket: Handling Errors

    Ticket: Handling Errors

    Task

    For this ticket, you'll be required to modify the following methods:

    MovieDao.java

    • validIdValue

    CommentDao.java

    • addComment
    • deleteComment
    • updateComment

    UserDao.java

    • addUser
    • createUserSession
    • deleteUser
    • updateUserPreferences

    Ensure that all of these methods are more robust and account for potential exceptions when executed.


    MFlix Functionality

    Once this ticket is completed, the app will be able to handle incorrect movie id values and various write exceptions without breaking or throwing an error within the application.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=HandlingErrorsTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    or run the Application.java from your IDE.

    opened by CSalih 0
  • Ticket: Handling Timeouts

    Ticket: Handling Timeouts

    Ticket: Handling Timeouts

    Task

    For this ticket, you'll be required to modify the connection information for MongoClient to set a write concern timeout of 2500 milliseconds.

    The MongoClient in mflix.config.MongoDBConfiguration is initialized in the mongoClient bean method. There are a few other details in the Mongo Client section of the Java Driver documentation for your reference.

    Aside from the write concern timeout, you are also tasked to set the connectTimeoutMS configuration option to 2000 milliseconds. This option should be set in the connection string. Check MongoDB URI options reference for more information.

    The unit test TimeoutsTest.java will be asserting that these two configuration options are correctly set.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=TimeoutsTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    Or run the Application.java class from your IDE.

    opened by CSalih 0
  • Ticket: Connection Pooling

    Ticket: Connection Pooling

    Ticket: Connection Pooling

    Task

    For this ticket, you'll be required to modify the configuration of option that defines our maxPoolSize in the application.properties file, and set the maximum size of the connection pool to 50 connections.

    By changing the properties file, the MongoClient should be configured to use no more than 50 connections. Revise the ConnectionString java class api.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=ConnectionPoolingTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    Or deploy the application from your IDE.

    opened by CSalih 0
  • Ticket: Migration

    Ticket: Migration

    Ticket: Migration

    Task

    For this ticket, you'll be completing a script that performs field data type migrations. The script main class implementation is src/main/java/mflix/Migrator.java.

    Things always change, and a requirement has come down that the lastupdated value in each document of the movies collection needs to be stored as an ISODate rather than a String.

    Apart from the lastupdated field, we also want to clean up the different data types that currently define the field imdb.rating, where in some cases it is set as of Number type, and in other cases set as String. Given that this field represents a numeric value, this field should be set as a number in all documents.

    Complete the script so it updates the values using the bulk API.

    To perform the migration, run the following command:

    mvn clean compile exec:java -Dexec.mainClass="mflix.Migrator"
    

    or run the Migrator.java class from your IDE.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=MigrationTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    
    opened by CSalih 0
  • Ticket: User Report

    Ticket: User Report

    Ticket: User Report

    User Story

    "As an administrator, I want to be able to view the top 20 users by their number of comments."


    Task

    For this ticket, you'll be required to modify the mostActiveCommenters method in CommentDao.java. This method produces a report of the 20 most frequent commenters on the MFlix site.

    Hint

    This report is meant to be run from the backend by a manager who is very particular about the accuracy of data. Ensure that the read concern used in this read, avoids any potential document rollback.

    Remember to add the necessary changes in the pipeline to meet the requirements. More information can be found in the comments of the method.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=UserReportTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    Or run Application.java from your IDE.

    opened by CSalih 0
  • Ticket: Delete Comments

    Ticket: Delete Comments

    Ticket: Delete Comments

    User Story

    "As a user, I want to be able to delete my own comments."


    Task

    For this ticket, you'll be required to modify the deleteComment method in CommentDao.java. Ensure the delete operation is limited so only the user can delete their own comments, but not anyone else's comments.


    MFlix Functionality

    Once this ticket is completed, users will be able to delete their own comments, but they won't be able to delete anyone else's comments.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=DeleteCommentTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    
    

    or run the Application.java from your IDE.

    opened by CSalih 0
  • Ticket: Create/Update Comments

    Ticket: Create/Update Comments

    Ticket: Create/Update Comments

    User Story

    "As a user, I want to be able to post comments to a movie page as well as edit my own comments."


    Task

    For this ticket, you'll be required to implement the addComment and updateComment methods in CommentDao.

    Ensure that updateComment only allows users to update their own comments, and no one else's comments.


    MFlix Functionality

    Once this ticket is completed, users will be able to post comments on their favorite (and least favorite) movies, and edit comments they've posted.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=UpdateCreateCommentTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    Our launch the Application from your IDE.

    opened by CSalih 0
  • Ticket: Get Comments

    Ticket: Get Comments

    Ticket: Get Comments

    User Story

    "As a user, I want to be able to view comments for a movie when I look at the movie detail page."


    Task

    For this ticket, you'll be required to extend the getMovie method in MovieDao.java so that it also fetches the comments for a given movie.

    The comments should be returned in order from most recent to least recent using the date key.

    Movie comments are stored in the comments collection, so this task can be accomplished by performing a $lookup. Refer to the Aggregation Quick Reference for the specific syntax.


    MFlix Functionality

    Once this ticket is completed, each movie's comments will be displayed on that movie's detail page.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=GetCommentsTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    Or run Application.java from your IDE.

    opened by CSalih 0
  • Ticket: User Preferences

    Ticket: User Preferences

    Ticket: User Preferences

    User Story

    "As a user, I want to be able to store preferences such as my favorite cast member and preferred language."


    Task

    For this Ticket, you'll be required to implement the updateUserPreferences method in UserDao.java. This method allows updates to be made to the "preferences" field in the users collection.


    MFlix Functionality

    Once this ticket is completed, users will be able to save preferences in their account information.


    Testing and Running the Application

    If the application is already running, stop the application and run the unit tests for this ticket by executing the following command:

    mvn test -Dtest=UserPreferencesTest
    

    Once the unit tests are passing, run the application with:

    mvn spring-boot:run
    

    or run the Application.java from your IDE.

    opened by CSalih 0
  • Ticket: Durable Writes

    Ticket: Durable Writes

    Ticket: Durable Writes

    User Story

    When a new user registers for MFlix, their information must be added to the database before they can do anything else. For this reason, we want to make sure that the data written by the addUser method will not be rolled back.

    We can decrease the chances of a rollback by increasing the write durability of the addUser method.


    Task

    For this ticket, you'll be required to increase the durability of the addUser method.

    opened by CSalih 0
Owner
Salih Candir
I'm a computer science student at the University of Innsbruck.
Salih Candir
Spring Boot & MongoDB Login and Registration example with JWT, Spring Security, Spring Data MongoDB

Spring Boot Login and Registration example with MongoDB Build a Spring Boot Auth with HttpOnly Cookie, JWT, Spring Security and Spring Data MongoDB. Y

null 15 Dec 30, 2022
Professional Java Developer Career Starter: Java Foundations Course Exercise Solutions

java-foundations-solutions Professional Java Developer Career Starter: Java Foundations Course Exercise Solutions The solutions are generally to be fo

Neutrino Systems Inc 41 Dec 28, 2022
CRUD operation using java springboot microservice hosted in kubernetes env, data stored in mongodb

springboot-mongodb-k8s-parth Brief Introduction Hello Friends, I have created REST API using Springboot and Spring cloud application which performs CR

Parth Shah 1 Nov 11, 2021
mall学习教程,架构、业务、技术要点全方位解析。mall项目(40k+star)是一套电商系统,使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5.0、MongoDB 4.2.5、Mysql5.7等技术,采用Docker容器化部署。

mall学习教程 简介 mall学习教程,架构、业务、技术要点全方位解析。mall项目(40k+star)是一套电商系统,使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5

macro 11.7k Jan 8, 2023
Springboot CRUD api using containerized mongoDB. ☕🍃📦

Javongo ☕ ?? Springboot CRUD api using containerized mongoDB. Feel free to use it as an example for your projects. Running Make sure ports 27017 & 808

Antonio Cituk 8 Mar 19, 2022
Ferramenta de Gerenciamento de Projetos de Software utilizando a metodologia Ágil Scrum e Gamification com Webflux e Mongodb

Task Flow Application Getting started To make it easy for you to get started with GitLab, here's a list of recommended next steps. Already a pro? Just

Rodolfo Gonçalves de Luna Freire 1 Oct 22, 2021
Allows you to use the MongoDB query syntax to query your relational database.

Spring Data JPA MongoDB Expressions How it works: Customize JPA Repository base class: @SpringBootApplication @EnableJpaRepositories(repositoryBaseCla

Muhammad Hewedy 86 Dec 27, 2022
How to configure Replica Set with Embedded Mongo using Spring Boot and Flapdoodle for unit testing code that uses mongodb transactions

Spring Boot Embedded Mongo with Replica Set This project defines a basic rest service that allows users to update records of a Person (name and email)

Divyansh Shekhar Gaur 4 Nov 1, 2022
Deploying Spring Boot and MongoDB as Containers Using Docker and Docker Compose

springboot-mongodb-docker Deploying Spring Boot and MongoDB as Containers Using Docker and Docker Compose Steps & Commands pull mongo image from docke

Java Techie 9 Nov 25, 2022
Alibaba Cloud Dedicated KMS Transfer SDK for Java can help Java developers to migrate from the KMS keys to the Dedicated KMS keys.

Alibaba Cloud Dedicated KMS Transfer SDK for Java Alibaba Cloud Dedicated KMS Transfer SDK for Java can help Java developers to migrate from the KMS k

Alibaba Cloud 3 May 12, 2022
Community-Driven Game Server Development solution for Java Developers based on DEEPINTHINK MagOKO Project.

MagOKO Stack Community-Driven Game Server Development solution for Java Developers based on DEEPINTHINK MagOKO Project. License Copyright 2021-present

DeepInThink Community 10 Jun 1, 2021
Rivr is a lightweight open-source dialogue engine enabling Java developers to easily create enterprise-grade VoiceXML applications.

Overview Rivr is a lightweight open-source dialogue engine enabling Java developers to easily create enterprise-grade VoiceXML applications. Read our

Nu Echo Inc. 57 Jun 27, 2022
Java based open source static site/blog generator for developers & designers.

JBake JBake is a Java based open source static site/blog generator for developers. Documentation Full documentation is available on jbake.org. Contrib

JBake 1k Dec 30, 2022
RR4J is a tool that records java execution and later allows developers to replay locally.

RR4J [Record Replay 4 Java] RR4J is a tool that records java execution and later allows developers to replay locally. The tool solves one of the chall

Kartik  kalaghatgi 18 Dec 7, 2022
A compact and highly efficient workflow and Business Process Management (BPM) platform for developers, system admins and business users.

Flowable (V6) Maven Central: Docker Images: License: Homepage: https://www.flowable.org/ flowable / flowəb(ə)l / a compact and highly efficient workfl

Flowable 6k Jan 7, 2023
These samples explore the different options that Spring Boot developers have for using Javascript and CSS on the client (browser) side of their application.

Table of Contents Getting Started Narrowing the Choices Create a New Application Webjars Show Me Some Javascript Normalizing Resource Paths Adding Tab

Dave Syer 18 Jul 29, 2022
Features useful for Minecraft content developers.

Easy Development A mod to make Minecraft content development easier. Includes features primarily to assist with mod, resource pack, and datapack devel

null 2 Feb 15, 2022
A simple and lightweight Minecraft GUI API for developers to use.

Current Version: 1.0.0 Requirements: - You must be using PaperMC or a fork of it (This will not work with just Spigot/Bukkit! - Curently this API only

Sammy Aknan 2 May 14, 2022
A powerful API whichs allows developers to change a players name and UUID on login.

UUIDSwitcher An easy to use but powerful API for spigot servers which gives developers control over the UUID and name a player logs in with. This chan

BeefDev 6 Nov 30, 2022