<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dev Guy on Medium]]></title>
        <description><![CDATA[Stories by Dev Guy on Medium]]></description>
        <link>https://medium.com/@DevGuy?source=rss-18115483a126------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*ksxF6ADbjv49zk7TYc2SHg.jpeg</url>
            <title>Stories by Dev Guy on Medium</title>
            <link>https://medium.com/@DevGuy?source=rss-18115483a126------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:46:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@DevGuy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Make the First Step to Start Working with Spring Data and MongoDB (NoSQL)]]></title>
            <link>https://medium.com/@DevGuy/how-to-make-the-first-step-to-start-working-with-spring-data-and-mongodb-nosql-90efdcb4d905?source=rss-18115483a126------2</link>
            <guid isPermaLink="false">https://medium.com/p/90efdcb4d905</guid>
            <category><![CDATA[spring-data]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[nosql]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Dev Guy]]></dc:creator>
            <pubDate>Sun, 22 Dec 2019 01:09:03 GMT</pubDate>
            <atom:updated>2020-06-24T00:14:12.550Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Make the First Step to Start Working with MongoDB (NoSQL) and Spring Data</h3><p><em>SQL to NoSQL in a Spring Framework Environment</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9dplkv9RWGQfeimsxFJidg.jpeg" /></figure><h3>Abstract</h3><p>Hello everyone, If you are familiar with Spring Data (Spring Boot + JPA/Hibernate) and <a href="https://en.wikipedia.org/wiki/Relational_database">RDBMS</a>, such as PostgreSQL/Microsoft SQL Server, etc.<br>This article should be a piece of cake for you to make the transition to the world of NoSQL database.<br>This article aims to explain how to configure to start working with MongoDB (NoSQL) in a Spring Boot Data in a few simple steps.</p><h3>0. Background</h3><p>We live in a world where companies are growing fast and need to have the ability to manage a massive amount of data that keeps growing every second. This data needs to be saved efficiently at any given moment. Moreover, due to the multiplicity of user’s devices, especially in the IoT field, which gaining momentum every year.<br>Because of this, I believe that during these days, besides the needs of the RDBMS to easily manage and organize entities, we also need NoSQL to save this high volume of data due to the big Data world we are living in as described above.<br>Therefore, I have decided that it is time for me to do this change as well and move to NoSQL World, you will be surprised how easy it is.</p><h3>1. How to install and configure MongoDB</h3><p>1.1. First, you should start by <a href="https://www.mongodb.com/download-center/community">download &amp; install</a> the “<em>MongoDB free community server” (</em>make sure you did not choose the MongoDB Enterprise Server<em>)</em>.<br>I chose version, OS, and package, as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*ukBtyDHU4HAQdjBm4ktA-g.png" /></figure><p>1.2. Choose Setup type “Complete” and then click next until the end of the installation process but make sure <em>MongoDB Compass is checked</em> (it should be checked by default).</p><p><strong>·</strong> Compass is the GUI tool for MongoDB, similar to pgAdmin with PostgreSQL.</p><h3>2. Optional Set DB on the cloud with Username and Password</h3><p>2.1. Go to <a href="https://cloud.mongodb.com/user#/atlas/register/accountProfile">MongoDB website,</a> and fill in your details to <em>Sign </em>Up.<br>2.2. On this screen, you will have to choose a cluster I prefer here the “Starter Cluster” one simply because it is free!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*olBPd7Pqr8yrAt037OgdZw.png" /></figure><p>2.3. On this screen, you will have to select one of the providers and a region. I went with Amazon AWS cloud, which I am familiar with, and chose Ireland as a server because it is the closest server available to me.</p><p><strong>·</strong> All the other settings I left as default.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*g5vJodkYRd8BenHzRMx5pw.png" /></figure><p>2.4. On this screen, click <em>Connect </em>as in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*yC5-dnovtz1-EgHyOT8VOA.png" /></figure><p>2.5. On this screen, you will have to set up the IP which will be allowed to access this MongoDB cluster I chose “<em>Add You Current IP Address</em>” then pick <em>Username </em>and <em>Password and </em>Click on the “<em>Create MongoDB user” </em>button and last<em> </em>Click on the “<em>choose a connection method”</em> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*yZ_Q8qv033wc9LxNy7a3dQ.png" /></figure><p>2.6. On the next screen, you will have to choose 1 of 3 ways to connect with your MongoDB, Choose the MongoDB Compass.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*_soFRnnKXBcOA3xsygaSjw.png" /></figure><p>2.7. In the next section, you should select “<em>I have Compass.” </em>and then<em> </em>click on the <em>copy </em>button to copy the entire MongoDB access URL and finally click on the “<em>Close</em>” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*OTES_eqA4JuBmnhke1iC_g.png" /></figure><p>2.8. Open <em>MongoDB Compass</em> on startup <em>Compass, and If</em> you chose to do step 2, you<em> would</em> prompt that it was able to identify automatically the MongoDB URL copied from our clipboard a make sure to fill your password and click connect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*B4vp3CR2uJgk5Naphsef3w.png" /></figure><p>Else you should, by default, connect to your localhost MongoDB without Username or password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sVH6C-iDemOTjFPkbhw4sw.png" /></figure><p>That’s it guys, we finish the MongoDB part, and now we need to connect it to Spring Boot which should be simple, let’s do it!</p><h3>3. Connect MongoDB to Spring Boot Application</h3><p>3.1. Create a <em>Spring boot project with Spring Data JPA and Spring Data MongoDB</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*M6Lx_LwYeBA2wO6pxXwf8Q.png" /></figure><p>3.2.1 Move this project to your favorite IDE, I use Eclipse in this example.</p><p>3.2.1 Add the connection details to application.properties file inside your project as shown below:<br>For localhost DB with no username or password:</p><pre>spring.data.mongodb.uri=mongodb://localhost:27017/example</pre><p>For AWS with username or password DB:</p><pre>spring.data.mongodb.uri=mongodb+srv://&lt;username&gt;:password&gt;@&lt;cluster-name&gt;-o5vbr.mongodb.net/TestDB</pre><p>3.3 I will make a simple example with only one class to make sure it run and working.</p><p>3.3.1 Create customer class with the annotations @Document to Identifies our class to be persisted to MongoDB, @Id to use id variable as a Primary Key:</p><pre>package com.database.MongoDB.beans;</pre><pre>import org.springframework.data.annotation.Id;<br>import org.springframework.data.mongodb.core.mapping.Document;</pre><pre><a href="http://twitter.com/Document">@Document</a><br>public class Customer {<br> <a href="http://twitter.com/Id">@Id</a><br> int id;<br> String firstName;<br> String lastName;</pre><pre>public Customer(int id, String firstName, String lastName) {<br>  this.id = id;<br>  this.firstName = firstName;<br>  this.lastName = lastName;<br> }</pre><pre>}</pre><p>3.3.2. Create a <em>CustomerRepository interface, and make sure to extend the MongoRepository like here:</em></p><pre>package com.database.MongoDB.repo;</pre><pre>import org.springframework.data.mongodb.repository.MongoRepository;<br>import org.springframework.stereotype.Repository;<br>import com.database.MongoDB.beans.Customer;</pre><pre><a href="http://twitter.com/Repository">@Repository</a><br>public interface CustomerRepository extends MongoRepository&lt;Customer, Integer&gt; {<br> public Customer findByFirstName(String firstName);</pre><pre>}</pre><p>3.3.3. Creating a Customer Repository bean in the main class and persist in the MongoDB by using the default CRUD (save):</p><pre>package com.database.MongoDB;</pre><pre>import org.springframework.boot.SpringApplication;<br>import org.springframework.boot.autoconfigure.SpringBootApplication;<br>import org.springframework.context.ApplicationContext;<br>import com.database.MongoDB.beans.Customer;<br>import com.database.MongoDB.repo.CustomerRepository;</pre><pre>@SpringBootApplication<br>public class MongoDbApplication {</pre><pre>public static void main(String[] args) {</pre><pre>ApplicationContext ctx = SpringApplication.run(MongoDbApplication.class, args);<br>System.out.println(&quot;Go&quot;);</pre><pre>CustomerRepository cr = ctx.getBean(CustomerRepository.class);<br>Customer c1 = new Customer(1, &quot;Name&quot;, &quot;LastName&quot;);<br>cr.save(c1);<br> }<br>}</pre><p>3.3.4. Check on your <em>MongoDB Compass</em> if the data was inserted by clicking on the <em>Refresh </em>button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sw6ZfS0Vs8HMX_6OBxensw.png" /></figure><h3>4. Conclusions</h3><p>4.1. So that it guys, to sum it up, we learned today how to connect our Spring Boot application with MongoDB in a few steps and how to store data in it.<br>I hope you enjoyed this article, I would be glad to get some feedback and if you like this tutorial article consider to give me some claps 👏🏽.</p><h3>5. MongoDB repository</h3><p>For the full example with more queries and rest controller class, visit my MongoDB repository.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90efdcb4d905" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>