SpringPortal #3 - MongoDB Atlas with Spring Boot
Published: 2022-11-27 17:16:52

---
title: SpringPortal #3 - MongoDB Atlas with Spring Boot
published: true
date: 2022-11-11 21:58:46 UTC
tags: 
canonical_url: https://24naman.hashnode.dev/springportal-3-mongodb-atlas-with-spring-boot
---

# MongoDB

**MongoDB** is a NoSQL database that uses JSON-like documents to map and store data from Java objects.

Spring framework provides a very intuitive way that allows effortless operability between Spring and MongoDB.

## Setting up Database in MongoDB Atlas

[MongoDB Atlas](https://www.mongodb.com/atlas/database) allows creating a free database of size 512 MB to try the Atlas. You can learn more at [MongoDB Free Database](https://www.mongodb.com/database/free).

1. Create a free account on the MongoDB atlas website.
2. You'll see a similar screen:

3. Start a new project.
4. You'll be asked for the project name and then the project owner. Select a name that you like and you can go with the default owner.
5. Now, build a Database by clicking the "Build a Database" button and selecting the "Shared" cluster.
6. You can now move forward with pretty much the other default values. I'm using the cluster name "SpringPortalCluster". ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668193459202/5wezxFwaD.png)

Now, Atlas will start provisioning your free cluster. But, still steps are left,

1. Create a username & password.
2. Add your current IP to the Atlas using the "Add My Current IP Address" button, which will allow only the specified IP address to connect to the cluster. ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668193891432/eZ3xt8iCA.png)
3. Complete the setup and connect it using MongoDB Atlas.
4. In MongoDB Atlas, create a database. ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668194283812/s7Yxf7MPa.png)
5. We'll use this database and collection to connect from Spring.

* * *

- If you don't want to use MongoDB Atlas, you can also create a database locally.

* * *

## Connecting MongoDB to Spring

1. For connecting your Spring project to MongoDB, you need to have the following dependency in your `pom.xml`.

2. Add `@EnableMongoRepositories` annotation to your application class.

3. Now, if you run the application, it'll try to connect to the MongoDB database but since we haven't told it the MongoDB connection string, it'll try `localhost:27017` which you can use if want to try to run the MongoDB server locally.
4. But, since we've already created an Atlas database we want to connect to it. We need to add an `application.properties` file to the project's resource directory if it is already not present there.
5. Add the `spring.data.mongodb.uri=` property, ![Screenshot 2022-11-12 021841.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668199748400/XV3CVPBOk.png)
6. You should be able to see the successful connection to the MongoDB server in the logs.

## Coding the MongoDB support

1. For using MongoDB in your Spring application, you need to do the following steps:

2. We'll add `@Document` annotation to our **Person** class:

3. Add a package named `repository`.

4. Add an interface that will extends `MongoRepository`,

5. If you run the application right now and there is no error, you will see a collection created in MongoDB with the name **person**.

6. Now that we set up the repository, we can use it in the service method.

7. In the above service, we've also set the ID. Spring set the ID automatically if it is an Object or String type. But for numerical type, we need to set it explicitly.

8. For generating the ID, we've created a method.

9. Now we just need to update our controller.

## Bringing it all together

1. Adding a person

2. Getting the person

3. Entry can also be viewed in MongoDB compass:

* * *

## GitLab

[Code Repository](https://gitlab.com/spring-hashnode/hello-spring)

[Article-specific commit](https://gitlab.com/spring-hashnode/hello-spring/-/commit/6f3f0b6ec8cf428f93d58b81368ed6cc7b5bd3a5)

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2104033689576056"
     crossorigin="anonymous"></script>