Source: Medium

MongoDB: My First Dive into MongoDB: A Beginner's Journey with Spring Boot and Docker

First off, a huge thank you to Amigoscode for their amazing tutorials and inspiration - without their guidance, I might not have taken the plunge into MongoDB! A few days ago, I decided to step out of my comfort zone and try something new: MongoDB. As a developer familiar with relational databases like MySQL, I was curious about NoSQL databases and their flexibility. MongoDB, a popular document-based NoSQL database, caught my attention for its ability to handle unstructured data and scale effortlessly. Paired with Spring Boot, it seemed like the perfect combo to explore. Here's how I got started, including some handy tools like Docker, and what I learned along the way. MongoDB is a NoSQL database that stores data in flexible, JSON-like documents instead of rigid tables with rows and columns. This makes it ideal for projects where data structures might evolve over time - like a student management system with varying fields (think favorite subjects or addresses). It's fast, scalable, and developer-friendly, which is why I chose it for my first NoSQL experiment. To get MongoDB running quickly, I turned to Docker - a tool that lets you run applications in isolated containers. Containers are lightweight, portable, and perfect for spinning up a database without messing with my local machine. I also used Docker Compose to simplify the setup, as it allows you to define and run multi-container applications with a single command. Here's the docker-compose.yml I used: To start MongoDB and a handy web-based UI called Mongo Express, I ran: The -d flag runs the containers in detached mode, so they hum along in the background. This command pulls the MongoDB image, sets it up with an admin user ( admin/password ), and maps port 27017 for database access. It also launches Mongo Express on port 8081 , giving me a visual way to inspect the database. Why Docker? It saved me from installing MongoDB locally, ensured consistency across environments, and let me tear everything down with a simple docker compose down when I was done. Once the containers were running, I wanted to poke around inside MongoDB. Since I'm on Windows, I used winpty to access the container's bash shell: Inside the container, I ran these commands to connect to the database and explore: This let me verify that my database ( springmongo ) was up and running. Why use the shell? It's a quick way to test connectivity and see what's happening under the hood. For a prettier view, I opened my browser and navigated to localhost:8081 . Mongo Express loaded up, showing my database and collections after I logged in with admin/password . This UI was a lifesaver for visualizing my data without typing commands - perfect for a beginner like me. With MongoDB humming, I built a simple Spring Boot application to manage student data. The project included: You can explore the full code on my GitHub repository . Here's a snippet of my application.properties to connect Spring Boot to MongoDB: The data.generation.enabled flag triggered my service to insert five sample students, each with unique details like favorite subjects and money spent on books. Why this setup? It let me test MongoDB's flexibility with nested objects ( Address ) and arrays ( favouriteSubjects ). This was just the beginning. I'm excited to dig deeper into MongoDB's aggregation framework, experiment with sharding for scalability, and maybe even try a more complex project. For now, I'm thrilled with how approachable MongoDB turned out to be - especially with Docker and Spring Boot smoothing the way. Check out the complete project on GitHub Repository . Feel free to fork it, play around, or let me know what you think!

Read full article »
Annual Revenue
$1.0-5.0B
Employees
5.0-10K
Dev Ittycheria's photo - President & CEO of MongoDB

President & CEO

Dev Ittycheria

CEO Approval Rating

76/100

Read more