<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rose</title>
    <description>The latest articles on DEV Community by Rose (@shadowphoenix).</description>
    <link>https://dev.to/shadowphoenix</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F417722%2F191378af-eeff-4eac-8b5f-7363bb7d3c36.png</url>
      <title>DEV Community: Rose</title>
      <link>https://dev.to/shadowphoenix</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shadowphoenix"/>
    <language>en</language>
    <item>
      <title>What Are The Characteristics of Quality Code?</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Thu, 17 Sep 2020 11:03:03 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/what-are-the-characteristics-of-quality-code-akd</link>
      <guid>https://dev.to/shadowphoenix/what-are-the-characteristics-of-quality-code-akd</guid>
      <description>&lt;p&gt;Nowadays, we heavily rely on software running all sorts of applications, from banking solutions to browsing through our social media. And, let's be honest, that last one is the most important of all these days. Because we are so dependent on software, companies have to make sure that the code runs smoothly at all times. For that, we need quality code. But what are the characteristics of quality code?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover Photo by &lt;a href="https://unsplash.com/@cdr6934?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Chris Ried&lt;/a&gt; on &lt;a href="https://unsplash.com/@cdr6934?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I have been &lt;a href="https://www.rosafiore.eu/category/operating-system/write-great-code/"&gt;reading&lt;/a&gt; the book series "Write Great Code" by Randall Hyde. The very first book of the series, &lt;a href="https://www.amazon.com/Write-Great-Code-2nd-Understanding-dp-171850036X/dp/171850036X/ref=mt_other?_encoding=UTF8&amp;amp;me=&amp;amp;qid="&gt;Understanding the Machine&lt;/a&gt;, starts with defining what great code actually is. Of course, Hyde's opinion strongly influences the definition of great code in this book. However, it has prompted me to take a deeper dive in the subject and see what other opinions are out there.&lt;/p&gt;

&lt;h2&gt;
  
  
  System Quality Attributes
&lt;/h2&gt;

&lt;p&gt;When googling with search terms like "code quality" and "characteristics of quality code" the result vary a lot. The primary reason: each piece of software has different requirements. The definition of quality code depends on the requirements for the software. Say, you have an application that has to be super quick, then high speed is the characteristic of quality code. In that case, it might not be an issue that the portability of the software is practically non-existent.&lt;/p&gt;

&lt;p&gt;There is no clear set of characteristic for quality code in place. However, you'll see that there is a set that many people in the software engineering industry agree upon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NMLfHabx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uw5nefxqa2rmj9ljdjmy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NMLfHabx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uw5nefxqa2rmj9ljdjmy.png" alt="What are the characteristics of quality code?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are just some of the common software quality attributes that students get taught during many software engineering courses. However, in the end, it all boils down to what the application requires. Since there are so many different quality attributes, you can always pick the ones that apply most for you. &lt;a href="https://en.wikipedia.org/wiki/List_of_system_quality_attributes"&gt;This Wikipedia page&lt;/a&gt; offers a list of system quality attributes that you can choose from for your next project.&lt;/p&gt;

&lt;h2&gt;
  
  
  In Short
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Great code is software written using a consistent and prioritised set of good software characteristics. In particular, great code follows a set of rules that guide the decision a programmer makes when implementing an algorithm as source code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Randall Hyde; Understanding the Machine&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hyde gives this quote in his first book, in the very first chapter. This quote perfectly summarises what quality code should be. So, what are the characteristics of quality code? That depends on your requirements and what you want out of your system.&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>What is your experience as/with software tester(s)?</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Wed, 09 Sep 2020 10:49:29 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/what-is-your-experience-as-with-software-tester-s-1lpb</link>
      <guid>https://dev.to/shadowphoenix/what-is-your-experience-as-with-software-tester-s-1lpb</guid>
      <description>&lt;p&gt;Software testers are often stereotyped by developers and vice versa. I'm curious to see what the community here thinks of software testers.&lt;/p&gt;

&lt;p&gt;So, the question for the devs: what's your experience with software testers? Do you work with the stereotypical software tester that wants to break your application at all cost, or do they have a different mindset?&lt;/p&gt;

&lt;p&gt;For the testers in our community, what's your experience being a software tester? Do you feel like you're getting stereotyped by the devs around you? Or do you stereotype the devs? ;) &lt;/p&gt;

</description>
      <category>discuss</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>What characterises quality code?</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Tue, 01 Sep 2020 05:47:54 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/what-characterises-quality-code-3a4a</link>
      <guid>https://dev.to/shadowphoenix/what-characterises-quality-code-3a4a</guid>
      <description>&lt;p&gt;We all want to write quality code for our projects, but what defines quality code to begin with?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>codequality</category>
    </item>
    <item>
      <title>The Road to My Ultimate Training System</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Fri, 28 Aug 2020 11:25:07 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/the-road-to-my-ultimate-training-system-1pkb</link>
      <guid>https://dev.to/shadowphoenix/the-road-to-my-ultimate-training-system-1pkb</guid>
      <description>&lt;p&gt;If you've seen me around, you might have noticed that there is one massive project I want to accomplish: building an operating system from scratch. However, my college got in the way many times as it always does, keeping me from putting serious effort into the project. So, with my degree in the bag, it is finally time to walk the road to My Ultimate Training System.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover Photo by &lt;a href="https://unsplash.com/@haydenwalker?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Hayden Walker&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Humble Beginnings
&lt;/h2&gt;

&lt;p&gt;So, how did I even decide to undertake this journey? Let me take you back to two years ago. I had just started my internship at the company where I'm still working. My task was to convert some internal tooling, consisting of multiple script files, into a whole application with front- and backend. At the start, I was researching software architectures which made me fall in love with the subject. Especially the microkernel architecture took my breath away.&lt;/p&gt;

&lt;p&gt;One day, when I was travelling back home by train, I was messaging with my best friend, Valentin. We were talking about this microkernel architecture I had just discovered that week. The following conversation ensued:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Valentin: "So, this microkernel architecture, du you know any examples of software that use it?"&lt;br&gt;
Me: "Hmm, I cannot give you any examples from the top of my head, but I'm quite sure there are plenty. I mean, the main feature is having a core system handling the communications and plugins having the business logic."&lt;br&gt;
Valentin: "Interesting..."&lt;br&gt;
Me: "There is still a lot I have to learn about this architecture, but damn, it's interesting!"&lt;br&gt;
Valentin: "That for sure it is!"&lt;br&gt;
A small moment of silence creeps in, the train passing by field after field.&lt;br&gt;
Me: "You know what.. I'm going to build my own operating system! From scratch!"&lt;br&gt;
Valentin: "Are you insane?! Do you know how long that'll take?!"&lt;br&gt;
Me: "Hahaha, yeah, it'll most likely be a 10-year project or so, but hell, why not!"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And so, the idea of building my operating system was born.&lt;/p&gt;

&lt;h2&gt;
  
  
  Road to My Ultimate Training System
&lt;/h2&gt;

&lt;p&gt;It's been two years since I've decided to walk the path of the operating system. But, as I said, college managed to get in the way of my plans. However, now that I finally have my degree, some free time has opened up. Still, diving in a project of this magnitude like a headless chicken isn't the way to go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W_FYnc1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/av377e30nt68g7bdr71j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W_FYnc1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/av377e30nt68g7bdr71j.jpg" alt="The Flight of the Headless Chicken by [Club Orlov](http://cluborlov.blogspot.com/2018/11/the-flight-of-headless-chicken.html)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently, I read a post on Dev.to on things to do when starting a side project. The first step stood out to me most: Announce Your Idea. Now, I have thrown my idea around here and there in some comments and on my blog, but I have never really sat down to &lt;strong&gt;think&lt;/strong&gt; and &lt;strong&gt;plan&lt;/strong&gt; what I want to do.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/niharrs" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BVOLusW1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--fj2iuuXh--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/382126/c4b1342e-743c-4b43-9bae-f98e27748491.jpg" alt="niharrs image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/niharrs/get-these-in-place-before-starting-your-next-side-project-38l8" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Get These In Place Before Starting Your Next Side Project 👀&lt;/h2&gt;
      &lt;h3&gt;Niharika Singh ⛓ ・ Aug 25 ・ 2 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#career&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;That was until a late evening. I was feeling pooped as hell and not motivated to do anything. I told my partner: "Bleeeehhh, I'm pooped; I cannot focus; I cannot do anything tonight; tonight &lt;em&gt;SUCKS&lt;/em&gt;!". He looked at me and almost literally picked me up from the bed. Before I knew, he'd put me on a chair in the living room with a book and notebook. Next, he squashed a pair of headphones on my head and set a vinyl in the player with some Bob Marley (don't ask me why, I don't know it either). Without realising, my brain was swinging along with Marley 10 minutes later and producing all sorts of strange creative stuff.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Path to My Operating System
&lt;/h3&gt;

&lt;p&gt;You don't "just" build an operating system. You need knowledge for that, and lots of it too. So, before I even dare start writing a single line of code for my operating system, I need to work on that brain! I managed to create a high level overview of what I think I need to learn to get started and in which order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learning about Computer Architecture and Organisation;&lt;/li&gt;
&lt;li&gt;Learning C Programming;&lt;/li&gt;
&lt;li&gt;Learning Assembly Language;&lt;/li&gt;
&lt;li&gt;Learning about Operating System Structure;&lt;/li&gt;
&lt;li&gt;Building MUTS!!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Gaining this knowledge won't fall out of the sky magically in my brain, unfortunately. Boy, would it be interesting for that to happen! But no, I need books for this, and I have started with the first one this week! I have got &lt;a href="https://www.rosafiore.eu/operating-system/book-list-for-muts/"&gt;a big list&lt;/a&gt; of &lt;a href="https://www.rosafiore.eu/operating-system/additional-books-for-muts/"&gt;all the books&lt;/a&gt; that I could find that would be interesting for this project. However, reading them all would take me... an eternity. So, I've tried to keep the list short, for now.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://nostarch.com/writegreatcode1_2e"&gt;Write Great Code - Understand the Machine&lt;/a&gt; by Randall Hyde&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nostarch.com/writegreatcode2_2"&gt;Write Great Code - Thinking Low-Level Writing High-Level&lt;/a&gt; by Randall Hyde&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504"&gt;C Programming - A Modern Approach&lt;/a&gt; by K.N. King&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nostarch.com/assembly2.htm"&gt;The Art of Assembly&lt;/a&gt; by Randall Hyde&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/Modern-Operating-Systems-Global-Paperback/dp/B00QAVC79K/ref=sr_1_5?dchild=1&amp;amp;keywords=modern+operating+systems&amp;amp;qid=1598612664&amp;amp;s=books&amp;amp;sr=1-5"&gt;Modern Operating Systems&lt;/a&gt; by Andrew Tanenbaum&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/tuhdo/os01"&gt;Operating Systems from 0 to 1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w7-WcWo7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dayft4amsxp3ck4anu0s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w7-WcWo7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dayft4amsxp3ck4anu0s.jpg" alt="Creepy Creative Creations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Show The World
&lt;/h3&gt;

&lt;p&gt;Still inspired, I'm now trying to announce my idea to you all! I hope this gives you a bit of background for my motives even to dare touch this project. Believe me, I'm scared to bits and almost shitting my pants. Still, I also feel exhilarated and motivated to work on this fantastic project. Even if it fails miserably, I'm confident that I will learn so much along the way that it won't be a waste of time.&lt;/p&gt;

&lt;p&gt;But more importantly, I want to share my strides and struggles with you all. The first idea I have is to keep writing articles and posting them on &lt;a href="https://www.rosafiore.eu/"&gt;my blog&lt;/a&gt;. Furthermore, I want to cross-post my articles to &lt;a href="https://dev.to/shadowphoenix"&gt;Dev.to&lt;/a&gt;, and maybe to Medium and other dev communities. I'm not too sure about the last two, but if you have any ideas on that, please do let me know!&lt;/p&gt;

&lt;p&gt;I also do have one other idea, but I'd like your opinion on that too. Would it be interesting if I stream my C and Assembly programming on Twitch, sowing my learning process and progress on the languages? Afterwards, I can upload the stream to &lt;a href="https://www.youtube.com/channel/UCgTGwso0l6VEr8R_4fqGVHg"&gt;my YouTube channel&lt;/a&gt;, so you can watch it later.&lt;/p&gt;

&lt;p&gt;Finally, I would like to ask you: Do you have any ideas on how you'd like to keep updated on projects like this? Things that I can do to expose it more, so I can hold myself accountable to stay motivated even during the tougher learning times. I'm curious to see what you think!&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Last Thoughts
&lt;/h2&gt;

&lt;p&gt;The road to My Ultimate Training System will be a long one, that is a given. Projects like these will have their ups and downs, but I'm hoping that I can stick to my plan. The learning possibilities are endless, which is what motivates me the most. Naturally, the development of my operating system will be open source. There is one question that always lingers in the back of my brain: Will I, or anyone, ever use my OS seriously? That, I won't know until I've "finished" it. However, I'd like to think that one day I'll run my OS on a device and use it for simple tasks. That is the end goal for now.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>computerscience</category>
      <category>announceyouridea</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Let's Get Started with Understanding the Machine</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Wed, 26 Aug 2020 12:17:10 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/let-s-get-started-with-understanding-the-machine-9md</link>
      <guid>https://dev.to/shadowphoenix/let-s-get-started-with-understanding-the-machine-9md</guid>
      <description>&lt;p&gt;Last week, it finally came in the mail! The 2nd edition of Randall Hyde's book Write Great Code - Understand the Machine. I am excited to study this book and gain more in-depth knowledge of computer organisation. So, let's get started with understanding the machine!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover Photo by &lt;a href="https://unsplash.com/@umby?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Umberto&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/electronic?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Write Great Code
&lt;/h2&gt;

&lt;p&gt;Writing great code is not as easy as it may sound. There are a lot of aspects to take into account. One of the important elements is understanding how a computer works. If you can utilise the computer's strengths and avoid its weaknesses, I'm confident that your applications will improve.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order to write great code, you need to know how to write efficient code, and to write efficient code, you must understand how computer systems execute programs and how abstractions in programming languages map to low-level hardware capabilities of the machine. - Randall Hyde&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This quote summarises Hyde's view on what it takes to write code. In the past, well over 20 years ago, there was only one way to learn the absolute best of coding techniques: learning the Assembly languages. The reason behind that is not because of the language itself. Instead, it is because you'll learn a ton about machine organisation.&lt;/p&gt;

&lt;p&gt;Machine organisation is a subset of computer architecture. It covers topics such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Low-level data types;&lt;/li&gt;
&lt;li&gt;Internal CPU organisations;&lt;/li&gt;
&lt;li&gt;Memory organisation;&lt;/li&gt;
&lt;li&gt;Peripherals;&lt;/li&gt;
&lt;li&gt;Computer communication.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Write Great Code - Understand the Machine covers machine organisation quite in detail, which is essential information for me to gain. Not only because it would allow me to write better code, but also because I need this knowledge to write my operating system. Everything I'll learn from this book, I'll share with you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FWaH6CAd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3j5zqgfs4x7106wadony.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FWaH6CAd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3j5zqgfs4x7106wadony.jpg" alt="Pen, paper, book: Let's study Understand the Machine!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started with Understanding the Machine
&lt;/h2&gt;

&lt;p&gt;So, what topics does Hyde cover in this book? It is quite a list, and they can all be related to the subjects of machine organisation. This list gives an outline of what the book covers. That is what I will write about in the many articles to come.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic Computer Data Representation

&lt;ul&gt;
&lt;li&gt;What do integers, characters, character sets and many more primal data types look like in a computer?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Basic Arithmetic &amp;amp; Bit Operations

&lt;ul&gt;
&lt;li&gt;How does the computer perform calculations and operations with bits only?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Memory (RAM)

&lt;ul&gt;
&lt;li&gt;How does a computer access memory, and what are the characteristics of memory performance?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Complex Computer Data Representation

&lt;ul&gt;
&lt;li&gt;What do composite data types and memory objects (e.g. pointers and arrays) look like in a computer?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Boolean Logic &amp;amp; Digital Design

&lt;ul&gt;
&lt;li&gt;How can I optimise the boolean expressions in &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;CPU Architecture

&lt;ul&gt;
&lt;li&gt;How can I write code in a "CPU friendly" manner?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;CPU Instruction Set Architecture

&lt;ul&gt;
&lt;li&gt;Why do some statements take longer to execute than others?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Computer Communication to the Outside World

&lt;ul&gt;
&lt;li&gt;How does the computer communicate with peripherals (e.g. hard drive and sound card)?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When I see this list of things I need to learn, it terrifies me. However, it also excites me. There is still so much out there that I'm eager to learn, but I need to go one step at the time. If you want to follow my journey, keep an eye out for more posts to come! In the meantime, I'll get started with understanding the machine.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>codenewbie</category>
      <category>learning</category>
    </item>
    <item>
      <title>How to Throw Custom Exceptions with Spring Boot</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Thu, 20 Aug 2020 19:14:38 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/how-to-throw-custom-exceptions-with-spring-boot-2dd4</link>
      <guid>https://dev.to/shadowphoenix/how-to-throw-custom-exceptions-with-spring-boot-2dd4</guid>
      <description>&lt;p&gt;In the process of building my blog, I want to be able to let the user know if something went wrong. It can vary from the standard error 404, a custom message, to my own custom exceptions. I'll show you how to throw custom exceptions with Spring Boot, so you can do it yourself next time!&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Exceptions
&lt;/h2&gt;

&lt;p&gt;First, I want to define what my exception response towards the client would look like. I'm doing this with a simple class called &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/BLOG-5-Add-Blog-Post/blog-backend/src/main/java/eu/rosafiore/blogbackend/exception/web/ExceptionResponse.java"&gt;ExceptionResponse&lt;/a&gt; and some properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ExceptionResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;details&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All the properties also have get methods, but I've left that out of the code snippet. Since I don't expect to be changing the property values, I haven't implemented any set methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  ExceptionResponseHandler
&lt;/h3&gt;

&lt;p&gt;With the class for ExceptionResponse done, I can now start with handling the thrown exceptions. For this, I've created the &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/BLOG-5-Add-Blog-Post/blog-backend/src/main/java/eu/rosafiore/blogbackend/exception/web/ExceptionResponseHandler.java"&gt;ExceptionResponseHandler&lt;/a&gt; that inherits the &lt;code&gt;ResponseEntityExceptionHandler&lt;/code&gt; class. It is also annotated with &lt;code&gt;@ControllerAdvice&lt;/code&gt;. This annotation allows me to create one class that handles all exceptions for every controller in my application. So, I can add all the exceptions to be handled in this class keeping it in one spot for maintainability. &lt;/p&gt;

&lt;p&gt;So, first things first, I want to have one generic method for handling any exception that is thrown by my application. Whatever happens, I still want to notify the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ControllerAdvice&lt;/span&gt;
&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExceptionResponseHandler&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntityExceptionHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleAllExceptions&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;WebRequest&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt; &lt;span class="n"&gt;exceptionResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;exceptionResponse&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, the ExceptionResponseHandler handles any exception that the application throws. It returns the timestamp when the error occurred, the message from the exception, and the request they used. That should provide the client-side application enough information to handle the rest towards the user, without giving away too much details of the backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Exceptions in Spring Boot
&lt;/h2&gt;

&lt;p&gt;With the general setup done, it is finally time to get to the custom exceptions in Spring Boot. For that, I need to create an exception class to begin with. In my case, I want to notify that the blog post being posted already exists. However, I am fairly certain that the duplication of items won't apply to just blog posts. Therefore, I made it a little more generic: &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/BLOG-5-Add-Blog-Post/blog-backend/src/main/java/eu/rosafiore/blogbackend/exception/web/ResourceAlreadyExistsException.java"&gt;ResourceAlreadyExistsException&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ResourceAlreadyExistsException&lt;/code&gt; class is a relatively simple one. It extends the &lt;code&gt;RuntimeException&lt;/code&gt; class, and you can add as many parameters to it as you like. I've kept it concise like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ResourceAlreadyExistsException&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;RuntimeException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ResourceAlreadyExistsException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"Resource with property %s and value %s already exists."&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
            &lt;span class="s"&gt;"Make sure to insert a unique value for %s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whenever I need to check for a unique resource, I can tell the user which specific property has what value that causes the error. Furthermore, I notify the user what action must be taken to avoid the error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing The Error Code
&lt;/h3&gt;

&lt;p&gt;For every exception, there is a suitable error code. However, in this case, I wasn't sure what to use. Naturally, I searched around and stumbled upon &lt;a href="https://stackoverflow.com/questions/3825990/http-response-code-for-post-when-resource-already-exists"&gt;a question&lt;/a&gt; on StackOverFlow discussing this very example.&lt;/p&gt;

&lt;p&gt;The accepted answer was the error 409 Conflict code. Using this code means that the request cannot be completed due to a conflict in the current state of the resource. However, when thinking about this meaning, I feel like this is not the correct error code in my case. The state of the resource itself isn't the issue.&lt;/p&gt;

&lt;p&gt;Another option is using error 400 Back Request, but that doesn't feel right either. This code suggests that the server doesn't understand the request being made, and therefore cannot process it. That isn't the case here either, because the server perfectly understands the request. &lt;/p&gt;

&lt;p&gt;A bit further down the post, I stumbled upon an interesting answer: using error 422 Unprocessable Entity. The following is said about this error code in &lt;a href="https://tools.ietf.org/html/rfc4918#section-11.2"&gt;WebDAV RFC 4918&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415 (Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The definition of this error code seems to fall in line with the situation I am facing. The server understands the request; it is a valid request, but is unable to process the entity. It cannot process the entity, because it is a duplicate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating the ExceptionResponseHandler
&lt;/h3&gt;

&lt;p&gt;Finally, I chose to use error 422 for my &lt;code&gt;ResourceAlreadyExistsException&lt;/code&gt;. Still, I need to hook up this error message to the &lt;code&gt;ExceptionResponseHandler&lt;/code&gt;. The extra method is very similar to the method I created earlier for handling all exceptions. In fact, you can easily copy-paste this method for all the exceptions you have. All you have to do, is changing the Exception class to your exception and change the &lt;code&gt;HttpStatus.&amp;lt;code&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ResourceAlreadyExistsException&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleResourceAlreadyExistsException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;ResourceAlreadyExistsException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;WebRequest&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt; &lt;span class="n"&gt;exceptionResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ExceptionResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDescription&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;exceptionResponse&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UNPROCESSABLE_ENTITY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Figuring out how to get the custom exceptions working in Spring Boot certainly was a fun one to uncover. I'm glad that this solution is reusable for multiple projects as well! Next up is figuring out how to write unit tests for the layers. Preferably, I want to write unit tests with mocks, so I can test the functionality without any dependencies.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>java</category>
      <category>learning</category>
    </item>
    <item>
      <title>Check for Unique Documents in MongoDB with Spring Boot</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Wed, 19 Aug 2020 22:47:15 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/check-for-unique-documents-in-mongodb-with-spring-boot-971</link>
      <guid>https://dev.to/shadowphoenix/check-for-unique-documents-in-mongodb-with-spring-boot-971</guid>
      <description>&lt;p&gt;Whilst figuring out how exceptions work, I wanted to make sure that the blog posts are always unique. But how do we do that? That's why I'll show you how to check for unique documents in MongoDB with Spring Boot.&lt;/p&gt;

&lt;h2&gt;
  
  
  @Indexed Annotation
&lt;/h2&gt;

&lt;p&gt;At first, I looked into the &lt;code&gt;@Indexed&lt;/code&gt; annotation from Spring Boot. This annotation will make a new index in MongoDB. You can add extra parameters within the annotation to make it do something more specific for you. In my case, I am using the &lt;code&gt;unique&lt;/code&gt; parameter to let MongoDB check if a specific field is unique. With that, my blogpost class looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Document&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;documentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;@Indexed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;endPoint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;@JsonFormat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"dd-MM-yyyy HH:mm"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;publishDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I test this configuration, nothing happens. I can still add blog posts with duplicate endpoints. Later, I come across &lt;a href="https://stackoverflow.com/questions/53006818/spring-boot-mongo-wont-create-index-with-the-index-annotation" rel="noopener noreferrer"&gt;this post&lt;/a&gt; on StackOverFlow, stating that I have to add the line &lt;code&gt;spring.data.mongodb.auto-index-creation=true&lt;/code&gt; to my &lt;code&gt;application.properties&lt;/code&gt;. So, I added the line, even completely dropped the database and restarted MongoDB, but to no avail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Query for Unique Documents
&lt;/h2&gt;

&lt;p&gt;After a couple hours of arguing with the &lt;code&gt;@Indexed&lt;/code&gt; annotation, I finally give in. Still, I want to find a way to check for unique documents in MongoDB with Spring Boot. So, I resort to another way of getting there: using a database query.  It might not be the quickest one, but it will suffice for now.&lt;/p&gt;

&lt;p&gt;Ideally, what I want the query to do, is tell me whether there is already a blogpost in my database with the same value for &lt;code&gt;endPoint&lt;/code&gt;. I start out working my way through the &lt;a href="https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.query" rel="noopener noreferrer"&gt;Spring Data Documentation for MongoDB&lt;/a&gt;, trying to figure out how to setup my query. After another search,, I come along the &lt;a href="https://docs.mongodb.com/manual/reference/operator/query/exists/" rel="noopener noreferrer"&gt;MongoDB Documentation&lt;/a&gt; on &lt;code&gt;$exists&lt;/code&gt;. One of their examples uses &lt;code&gt;$exists&lt;/code&gt; combined with &lt;code&gt;$nin&lt;/code&gt; to collect documents where a field exists and its value does not equal to what you've set it to.&lt;/p&gt;

&lt;h3&gt;
  
  
  $exists and $nin
&lt;/h3&gt;

&lt;p&gt;Brilliant! Let's try that out. First, I start with creating a boolean to store my result in with the following bit of code. Then I use the boolean in my if-statement to decide whether to save the object or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;postExistsAlready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;matching&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"endPoint"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;nin&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEndPoint&lt;/span&gt;&lt;span class="o"&gt;()))).&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;postExistsAlready&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Posted!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Post already exists!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I test again, and, of course, it didn't work. With no errors appearing in my console, I resorted to PowerShell and getting right into MongoDB. After logging in, I check what data is already present in my database collection by using &lt;code&gt;db.blogposts.find()&lt;/code&gt;. Then, I try the MongoDB version of my query with the following line: &lt;code&gt;db.blogpost.find(endPoint:{$exists:true, $nin:"/test"}})&lt;/code&gt;. It might have been a bit late when I was working on this, because there is clearly a typo. However, I did find out one reason why nothing was happening, because I got an error when trying to execute the query.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkskdm6oln95mlg9zi3zn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkskdm6oln95mlg9zi3zn.png" alt="Error message from mongo: $nin needs an array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apparently, &lt;code&gt;$nin&lt;/code&gt; requires an array as input, rather than just a string. Fortunately, that is fairly easy to fix! With a little adjustment of the code, it now looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;endPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEndpoint&lt;/span&gt;&lt;span class="o"&gt;()};&lt;/span&gt;

    &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;postExistsAlready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;matching&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"endPoint"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;nin&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endPoint&lt;/span&gt;&lt;span class="o"&gt;))).&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;postExistsAlready&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Posted!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Post already exists!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome! Let's try that again. No luck either.&lt;/p&gt;

&lt;h3&gt;
  
  
  $exists and $in
&lt;/h3&gt;

&lt;p&gt;At this point, I start questioning whether I'm using the correct query methods, especially considering that there will be many more blog posts to come. On a hunch, I fiddled around in mongo with PowerShell. Instead of using &lt;code&gt;$nin&lt;/code&gt;, I tried &lt;code&gt;$in&lt;/code&gt; with a count method. So, I tried the following query in my PowerShell: &lt;code&gt;db.blogposts.count({"endPoint":{$exists:true,$in:["/test"]}})&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnrm0dkeal2m8vq59e5ns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnrm0dkeal2m8vq59e5ns.png" alt="Query Works!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, look at that! When I use &lt;code&gt;$nin&lt;/code&gt;, my count returns 0, because there are no documents in my database that don't have &lt;code&gt;/test&lt;/code&gt; as their endpoint. However, when I use &lt;code&gt;$in&lt;/code&gt;, the query returns me the number of articles that &lt;em&gt;do&lt;/em&gt; have &lt;code&gt;/test&lt;/code&gt; as their endpoint. That is exactly what I need!&lt;/p&gt;

&lt;p&gt;So, I adjusted my code again to use this information. The final version so far, looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;endPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEndPoint&lt;/span&gt;&lt;span class="o"&gt;()};&lt;/span&gt;

    &lt;span class="nc"&gt;Query&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addCriteria&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Criteria&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"endPoint"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

    &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;postExistsAlready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;postExistsAlready&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Posted!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Post Already Exists!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It took a bit to figure it all out, and I am not yet done with the &lt;code&gt;@Indexed&lt;/code&gt; annotation. Using a query like this works for the time being, but ultimately I think it would be better to use what MongoDB has available for us. I have seen ways to add the index manually, but it requires more research. For now, I'm happy to have solved this little riddle.&lt;/p&gt;

&lt;p&gt;Now, I do agree that these &lt;code&gt;System.out.println()&lt;/code&gt; statements are far from pretty. There is no way to notify the user that something has gone wrong. So, the next step is to figure out how to handle exceptions being thrown!&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>mongodb</category>
      <category>java</category>
      <category>learning</category>
    </item>
    <item>
      <title>Adding Functionality to the Backend - Building a Blog</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Fri, 14 Aug 2020 22:27:05 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/adding-functionality-to-the-backend-building-a-blog-3g5g</link>
      <guid>https://dev.to/shadowphoenix/adding-functionality-to-the-backend-building-a-blog-3g5g</guid>
      <description>&lt;p&gt;The proof of concept worked like a charm. Now, I need to create the basis by adding functionality to the backend. today, I'll start with creating a blog post and see what challenges lie ahead on the road.&lt;/p&gt;

&lt;p&gt;So, before I start implementing the full feature of creating a blog post. I had to remove the testing code I created last time in my proof of concept. With a clean &lt;code&gt;BlogPostController&lt;/code&gt;, I can further structure my Spring Boot project.&lt;/p&gt;

&lt;h2&gt;
  
  
  High-Level Project Structure
&lt;/h2&gt;

&lt;p&gt;I want to keep my business logic separated from my controller layer, even though there might not be a whole lot of business logic to a blog backend. Between my controller and repository layer, I add a service layer. So, essentially, we get the following structure in my project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hht2Uvv_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fqa53mfg3yyzhtwmrg23.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hht2Uvv_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fqa53mfg3yyzhtwmrg23.png" alt="High-Level Project Structure for Adding Functionality"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, I'm using a &lt;code&gt;BlogPostService&lt;/code&gt; interface and a &lt;code&gt;BlogPostServiceImpl&lt;/code&gt; for the implementation class. In this case, I'd like to use interfaces to future proof my software in case I need to change something in the implementation of my service. At school, I never really understood why, but &lt;a href="https://softwareengineering.stackexchange.com/questions/159813/do-i-need-to-use-an-interface-when-only-one-class-will-ever-implement-it"&gt;this post&lt;/a&gt; on StackExchange has helped me understand this better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Functionality
&lt;/h2&gt;

&lt;p&gt;Personally, I'd like to work from bottom to top when it comes to adding new functionality to my backend. Right now, I want to start with the first CRUD operating: create. I expect my frontend to deliver a blog post object in the request body, therefore I'musing my blogpost object in my code.&lt;/p&gt;

&lt;h3&gt;
  
  
  BlogPostService Interface
&lt;/h3&gt;

&lt;p&gt;So, building from bottom to top, I start with my &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/BLOG-5-Add-Blog-Post/blog-backend/src/main/java/eu/rosafiore/blogbackend/blogpost/BlogPostService.java"&gt;&lt;code&gt;BlogPostService&lt;/code&gt;&lt;/a&gt; interface first. For now, the interface is still quite empty, but that will change soon enough when adding more functionality of course. For now, my interface looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CreateBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  BlogPostService Implementation
&lt;/h3&gt;

&lt;p&gt;With the interface in place, I now have to write the implementation of the method in my &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/BLOG-5-Add-Blog-Post/blog-backend/src/main/java/eu/rosafiore/blogbackend/blogpost/BlogPostServiceImpl.java"&gt;&lt;code&gt;BlogPostServiceImpl&lt;/code&gt;&lt;/a&gt; with the same bit of code I used last time for MongoDB. Also,I have annotated the class with &lt;code&gt;@Service&lt;/code&gt; to let Spring Boot know that this is a service component within my application. Furthermore, I have autowired MongoTemplate, so I can use it freely for saving my blog post to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostServiceImpl&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;MongoTemplate&lt;/span&gt; &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CreateBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  BlogPostController
&lt;/h3&gt;

&lt;p&gt;Finally, I can write the code for my controller layer. I've added a couple annotations to my &lt;code&gt;BlogPostController&lt;/code&gt; versus last time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/blog"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="n"&gt;blogPostService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;
    &lt;span class="nd"&gt;@ResponseStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CREATED&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;blogPostService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CreateBlogPost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's go through the annotations. I have started with adding &lt;code&gt;@RestController&lt;/code&gt; above my class to notify Spring Boot that this is a controller component, specifically one that accepts REST requests. Furthermore, I have added a custom endpoint set with &lt;code&gt;@RequestMapping("/blog")&lt;/code&gt;. That means that every endpoint within this controller is &lt;code&gt;http://localhost:8080/blog&lt;/code&gt;. For every method, I have to define what type of method it is with the &lt;code&gt;@PostMapping&lt;/code&gt; annotation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to change the endpoint per method, you can add the name of the endpoint as a string in brackets behind &lt;code&gt;@PostMapping&lt;/code&gt; like this: &lt;code&gt;@PostMapping("/test")&lt;/code&gt;. The full endpoint for this method will be &lt;code&gt;http://localhost:8080/blog/test&lt;/code&gt;. Of course, you can do this for all request types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The last annotation, &lt;code&gt;@ResponseStatus(HttpStatus.CREATED)&lt;/code&gt; allows me to define what response I want my backend to send back. At this stage, I haven't yet figured out how to throw the exceptions. I still have to figure that out, and when I do, I'll write about it of course. For now, this will have to do and it serves its testing purpose.&lt;/p&gt;

&lt;p&gt;Last, but definitely not least, I have used te &lt;code&gt;@RequestBody&lt;/code&gt; annotation before my BlogPost variable. This annotation lets Spring Boot know to check in the request body for the blog post object, which the service uses to save the post to the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing My Feature
&lt;/h2&gt;

&lt;p&gt;Excited with my new bits of code, I was eager to test if it works. I run my first test with curl, since I don't have Postman available on my Debian laptop. Failed. Damn it! I check what I tried to send as my request body, and I noticed that I misspelled endpoint.There must be a way to validate the object to get a cleaner error message, right?&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Bean Validation
&lt;/h3&gt;

&lt;p&gt;After a quick search, I came across two articles (&lt;a href="https://www.baeldung.com/javax-validation"&gt;this one&lt;/a&gt; and &lt;a href="https://www.baeldung.com/javax-validation"&gt;this one&lt;/a&gt;) from Baeldung on Java Bean Validation. With a simple annotation, the bean automatically validates the input in the request body to match the object. I only have to make some tiny adjustments.&lt;/p&gt;

&lt;p&gt;First, I have to add two dependencies to my &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/BLOG-5-Add-Blog-Post/blog-backend/pom.xml"&gt;pom.xml&lt;/a&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.hibernate.validator&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;hibernate-validator&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.hibernate.validator&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;hibernate-validator-annotation-processor&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After letting maven do its thing, I only have to add the &lt;code&gt;@Valid&lt;/code&gt; annotation before the request body annotation, and that's it! Now, the function should work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Date Format
&lt;/h3&gt;

&lt;p&gt;Yeah, not a chance. The function still doesn't work. This time, I'm getting a parsing error on the data I input. Fortunately, &lt;a href="https://www.baeldung.com/spring-boot-formatting-json-dates"&gt;Baeldung&lt;/a&gt; came to the rescue again with a little article on the JsonFormat annotation. I added a small adjustment to my BlogPost class with the following line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@JsonFormat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"dd-MM-yyyy HH:mm"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;publishDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Functionality Added!
&lt;/h2&gt;

&lt;p&gt;At long last, I am able to post a blog post! It is far from perfect yet, as I still have to figure out how to get the exceptions to work. Furthermore, I still have to write unit tests as well to make sure that my code is working as expected. That is what I'll be working on the upcoming few days. Once I get these two parts done, I can continue with adding even more functionality like getting and deleting a specific or all posts.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>learning</category>
      <category>java</category>
    </item>
    <item>
      <title>Insert Data in MongoDB with Spring Boot - Building a Blog</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Thu, 06 Aug 2020 09:12:29 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/insert-data-in-mongodb-with-spring-boot-building-a-blog-29p4</link>
      <guid>https://dev.to/shadowphoenix/insert-data-in-mongodb-with-spring-boot-building-a-blog-29p4</guid>
      <description>&lt;p&gt;Last time, we set up our database and Spring Boot project, and secured our database credentials. Now, we have to configure MongoDB in our project. Then we can start to insert data in mongodb and getting a feel for the queries to get the data from the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Spring Boot to Access MongoDB
&lt;/h2&gt;

&lt;p&gt;To get started, we need to configure our Spring application, so our application knows where the database is to begin with. We do that by adding the following lines to the &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/resources/application.properties" rel="noopener noreferrer"&gt;&lt;code&gt;application.properties&lt;/code&gt;&lt;/a&gt; file in the resources directory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=rosafiore
spring.data.mongodb.authentication-database=admin


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When configuring MongoDB, we added a user to our database. This user uses the admin database to authenticate, and thus to access the other databases. That's why we need to define the authentication database, rather than just inputting the credentials to the database we will be talking to.&lt;/p&gt;

&lt;p&gt;However, we still have to insert our secured credentials. Truth be told, it took a long time to get this one figured out. There was no clear resource online that I could find. So, after a lot of digging and trying, I have managed to create a mongo configuration that inserts the secured credentials and creates a connection to the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  MongoConfig
&lt;/h3&gt;

&lt;p&gt;So, let's get started with this new config file. First off, we need to create a new class in our project. I call it &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/java/eu/rosafiore/blogbackend/config/MongoConfig.java" rel="noopener noreferrer"&gt;&lt;code&gt;MongoConfig&lt;/code&gt;&lt;/a&gt;, because it contains the configuration for MongoDB. We have to annotate the class with the &lt;code&gt;@Configuration&lt;/code&gt; and &lt;code&gt;@EnableMongoRepositories&lt;/code&gt;. That way, Spring Boot knows that this is a configuration class, and thus must be taken along during initialization and to enable the repositories. Furthermore, we need to inherit the &lt;code&gt;AbstractMongoClientConfiguration&lt;/code&gt; for a couple of methods to complete our configuration. So, our bare config class now looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableMongoRepositories&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MongoConfig&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractMongoClientConfiguration&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getDatabaseName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this class, we will create our own MongoClient to insert data in mongodb. For this, we need the properties we've defined in our &lt;code&gt;application.properties&lt;/code&gt; file. We can access the properties by using a spring annotation on our local variable: &lt;code&gt;@Value("${&amp;lt;property name&amp;gt;}")&lt;/code&gt;. With this construction, we create our variables with our property values and use them for configuring our client. One such variable will look like this in code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${spring.data.mongodb.database}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you might have seen a bit before, the MongoConfig class inherited the method &lt;code&gt;getDatabaseName()&lt;/code&gt;. There, we have to replace the &lt;code&gt;null&lt;/code&gt; value with the &lt;code&gt;database&lt;/code&gt; variable. Now, we need to dive in to configuring the MongoClient itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  MongoClientSettings
&lt;/h3&gt;

&lt;p&gt;After searching for some time, I came across &lt;a href="https://mongodb.github.io/mongo-java-driver/3.12/driver/tutorials/connect-to-mongodb/" rel="noopener noreferrer"&gt;the mongodb documentation&lt;/a&gt; for connecting to a database. Further down the page, I finally found what I was looking for: applying custom settings. I decided to create a separate private method to configure the settings to keep things tidy. Here is the code for my settings and I'll walk you through the code as we go.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;MongoClientSettings&lt;/span&gt; &lt;span class="nf"&gt;setMongoClientSettings&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;MongoCredential&lt;/span&gt; &lt;span class="n"&gt;credential&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MongoCredential&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createCredential&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;USERNAME&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;authDatabase&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PASSWORD&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;MongoClientSettings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;credential&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;credential&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;applyToClusterSettings&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ServerAddress&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;))))&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I start off with defining the credentials and the authentication database for my client. That is where I get to use my secured database credentials, instead of hardcoding them.&lt;/p&gt;

&lt;p&gt;Then comes the &lt;code&gt;MongoClientSettings.builder()&lt;/code&gt;, that is where the actual settings are created. The first thing I do, is setting the credentials to the one I created earlier in the method. Next, I need to configure the location of the database in the &lt;code&gt;applyToClusterSettings&lt;/code&gt;. Here, I set the host to a new ServerAddress with the host and port number. Finally, the building can create the settings and return those. The settings are then used to create the MongoClient for Spring Boot to use.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;MongoClient&lt;/span&gt; &lt;span class="nf"&gt;mongoClient&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;MongoClients&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;setMongoClientSettings&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, the MongoConfig class now looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableMongoRepositories&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MongoConfig&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractMongoClientConfiguration&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${spring.data.mongodb.host}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${spring.data.mongodb.port}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${spring.data.mongodb.database}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${spring.data.mongodb.authentication-database}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;authDatabase&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getDatabaseName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;MongoClient&lt;/span&gt; &lt;span class="nf"&gt;mongoClient&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;MongoClients&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;setMongoClientSettings&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;MongoClientSettings&lt;/span&gt; &lt;span class="nf"&gt;setMongoClientSettings&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;MongoCredential&lt;/span&gt; &lt;span class="n"&gt;credential&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MongoCredential&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createCredential&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;USERNAME&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;authDatabase&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PASSWORD&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;MongoClientSettings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;credential&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;credential&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;applyToClusterSettings&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                        &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ServerAddress&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;))))&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Insert Data in MongoDB
&lt;/h2&gt;

&lt;p&gt;With our configuration completed, it is time to set up the rest of the code to insert data in mongodb. For this, we first need to create a class that is basically the blueprint of our object. In my case, I want to start out with a simple blog post object and the data I expect in it.&lt;/p&gt;
&lt;h3&gt;
  
  
  BlogPost Model
&lt;/h3&gt;

&lt;p&gt;As I explained in &lt;a href="https://dev.to/shadowphoenix/building-a-blog-choosing-a-database-sql-or-nosql-1bea"&gt;Choosing a Database: SQL or NoSQL?&lt;/a&gt;, my blogpost object contains the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EndPoint&lt;/li&gt;
&lt;li&gt;Publish Date&lt;/li&gt;
&lt;li&gt;Categories&lt;/li&gt;
&lt;li&gt;Tags&lt;/li&gt;
&lt;li&gt;Blog Post (the content)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I reflected these properties in my &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/java/eu/rosafiore/blogbackend/blogpost/BlogPost.java" rel="noopener noreferrer"&gt;BlogPost class&lt;/a&gt; which looks like this (I removed the getters and setters to avoid clutter):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Document&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;documentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;@Indexed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;endPoint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;publishDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, I annotated the class with the &lt;code&gt;@Document&lt;/code&gt; annotation. That allows me to choose the name of the collection where mongodb will be storing my blog posts. Furthermore, I have created a &lt;code&gt;documentId&lt;/code&gt; with the &lt;code&gt;@Id&lt;/code&gt; annotation to inform mongodb that this property will contain the ID for every blog post object stored in the database. Finally, I have added the &lt;code&gt;@Indexed(unique = true)&lt;/code&gt; annotation to my EndPoint property. This annotation allows me to have mongodb check if the property is actually unique. I don't want duplicate endpoints for my blog posts after all!&lt;/p&gt;

&lt;h3&gt;
  
  
  BlogPost Repository and Controller
&lt;/h3&gt;

&lt;p&gt;With the model complete, it is time to insert the first blog post in the database. For this, we need a Repository first. Fortunately, this is a relatively simple one, I only need to create the interface and that is about it for now.&lt;/p&gt;

&lt;p&gt;With that, the &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/java/eu/rosafiore/blogbackend/blogpost/BlogPostRepository.java" rel="noopener noreferrer"&gt;BlogPostRepository&lt;/a&gt; looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;BlogPostRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MongoRepository&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That is it for the repository, now we can continue with the &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/java/eu/rosafiore/blogbackend/blogpost/BlogPostController.java" rel="noopener noreferrer"&gt;controller&lt;/a&gt;. The controller contains the endpoints for our requests. I've made this a very simple controller, because it is still a proof of concept for me. I created two methods: one for posting a blog post and one for getting a blog post. For the POST method, I have hardcoded the values for the blog post.&lt;/p&gt;

&lt;p&gt;In order to insert data into mongodb, I'll be using MongoTemplate which I only have to autowire into the application like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@Autowired&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;MongoTemplate&lt;/span&gt; &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With the mongoTemplate autowired, creating the POST and GET method was relatively easy and straightforward.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createPost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="n"&gt;blogPost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setEndPoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/test"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPublishDate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCategories&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"TestCategory"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setTags&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"TestTag"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setArticle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testPost.md"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blogPost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"blogposts"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getPost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Query&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addCriteria&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Criteria&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"endPoint"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/test"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mongoTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Of course, the only way to know for sure, is to test it. So, I went into Postman, inserted the requests and finally, after a long days struggle I get the following response when trying to obtain the blog post I had inserted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fimqv48hzyqizwdsp83i0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fimqv48hzyqizwdsp83i0.png" alt="Getting my test post back from the database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that, I have finally managed to insert data in mongodb! Now, I can start with expanding the backend code with more endpoints. Furthermore, I need to decide how to proceed with this project and what steps I will take next.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>learning</category>
      <category>java</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>Securing Database Credentials - Building a Blog</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Fri, 17 Jul 2020 20:37:36 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/securing-database-credentials-building-a-blog-1630</link>
      <guid>https://dev.to/shadowphoenix/securing-database-credentials-building-a-blog-1630</guid>
      <description>&lt;p&gt;With our database set up, it is now time to make a MongoDB connection with Spring Boot. That connects the database to our backend system, making us ready to start implementing our operations. However, I don't want my database credentials to be all over GitHub. Therefore, in this post, I will be securing my database credentials.&lt;/p&gt;

&lt;p&gt;I already had my Spring Boot project, but in case you don't, go to &lt;a href="https://start.spring.io/"&gt;https://start.spring.io/&lt;/a&gt;. Here, you can easily configure your start project with Spring Boot. For my project, I have selected the following dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Boot DevTools&lt;/li&gt;
&lt;li&gt;Spring Web&lt;/li&gt;
&lt;li&gt;Spring Security (I want to use JTW tokens later on in the project)&lt;/li&gt;
&lt;li&gt;Spring Data MongoDB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let the website generate a project for you, unzip it and you should start with a blank Spring Boot project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Securing Database Credentials
&lt;/h2&gt;

&lt;p&gt;The first thing I want to do, is securing my database credentials. One of the places to put these, is in the application.properties file. However, this file gets uploaded to GitHub, which would mean that the database credentials would be visible to everyone. And, lets be honest, I don't want that and you probably don't want that either. So, it is time to implement a solution that would solve this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the Properties File
&lt;/h3&gt;

&lt;p&gt;Java does have a sweet feature where you can tap into the system properties. In this case, I want to use the method &lt;code&gt;System.getProperty("user.home")&lt;/code&gt;. This method allows me to go to the home drive without hard-coding it. On Windows, the &lt;code&gt;user.home&lt;/code&gt; directory is usually located at &lt;code&gt;C:\Users\&amp;lt;your username&amp;gt;\&lt;/code&gt;. On Linux, it is the home directory. Using this method, we can make the software cross platform as well. I have created a subdirectory that will contain files for my project. &lt;/p&gt;

&lt;p&gt;So, in this subdirectory, I created my properties file: &lt;code&gt;rosafiore.properties&lt;/code&gt;. In this file, I have set the username and password I'm using for my database connection. It looks somewhat like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username=&amp;lt;username&amp;gt;
password=&amp;lt;password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that I am &lt;em&gt;not&lt;/em&gt; using any accolades for the string I use to define my username and password. Now, it is time to get back to the project in IntelliJ (or any other Java IDE you prefer). I have created the following file structure for my project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vZck6VPe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/grejgnbo6z6skpmflpgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vZck6VPe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/grejgnbo6z6skpmflpgi.png" alt="Project Structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading the Property File on StartUp
&lt;/h3&gt;

&lt;p&gt;The first class I'm going to take a look at is the Config class. I use this class to load the properties from my property file with the &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/java/eu/rosafiore/blogbackend/config/Config.java"&gt;following bit of code&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@ConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"config"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Validated&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;USERNAME&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;PASSWORD&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;PROPERTIES_LOCATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user.home"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
            &lt;span class="s"&gt;"/RosaFiore/rosafiore.properties"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;getConfig&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Properties&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Properties&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;load&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileInputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PROPERTIES_LOCATION&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
            &lt;span class="no"&gt;USERNAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="no"&gt;PASSWORD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can now load the properties from our properties file! However, we need this to be done on startup of the application. So, we need to find a way to tap into the Spring Boot startup process and run this Config class. For this, we need to make some changes in the class where the main method resides.&lt;/p&gt;

&lt;p&gt;In the class with our main method, I'm going to add an inner static class. This inner class allows us to use the &lt;code&gt;getConfig()&lt;/code&gt; from our Config class. We can achieve that by creating a class that implements an ApplicationListener. To be more specific, a listener that triggers on the &lt;code&gt;ApplicationEnvironmentPreparedEvent&lt;/code&gt;. By overriding &lt;code&gt;onApplicationEvent()&lt;/code&gt;, we can use &lt;code&gt;Config.getConfig()&lt;/code&gt; to load our properties. Then, we have to adjust our main method to add the ApplicationListener, so it triggers our &lt;code&gt;Config.getConfig()&lt;/code&gt; on the environment prepared event. Fully finished, it looks somewhat like &lt;a href="https://github.com/Rosaa-xd/rosafiore/blob/mongodbConnection/blog-backend/src/main/java/eu/rosafiore/blogbackend/BlogBackendApplication.java"&gt;this&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogBackendApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnvironmentPreperation&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ApplicationListener&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ApplicationEnvironmentPreparedEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;@Override&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onApplicationEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ApplicationEnvironmentPreparedEvent&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConfig&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;USERNAME&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PASSWORD&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IncompleteConfigException&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt; &lt;span class="n"&gt;blogBackend&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;blogBackend&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addListeners&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BlogBackendApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;EnvironmentPreperation&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;blogBackend&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setSources&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogBackendApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
        &lt;span class="nc"&gt;ConfigurableApplicationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blogBackend&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DbUsername: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;USERNAME&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" ; DbPassword: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PASSWORD&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have added that last line of code to make sure that the properties are loaded correctly. When running the code, I see this beautiful piece of log appearing in my console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2020-07-17 20:07:05.519  INFO 14676 --- [  restartedMain] e.r.blogbackend.BlogBackendApplication   : Started BlogBackendApplication in 2.4 seconds (JVM running for 2.981)
DbUsername: &amp;lt;username&amp;gt; ; DbPassword: &amp;lt;password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that I have secured my database credentials, I can start working on connecting my MongoDB database to my Spring Boot project. So, next time, I will start implementing and adding my first test blog posts into the database.&lt;/p&gt;

</description>
      <category>java</category>
      <category>learning</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How do you keep up with writing?</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Thu, 16 Jul 2020 15:18:25 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/how-do-you-keep-up-with-writing-1d1j</link>
      <guid>https://dev.to/shadowphoenix/how-do-you-keep-up-with-writing-1d1j</guid>
      <description>&lt;p&gt;I wonder if it's me or not, but I often find myself having a lack of motivation to write. Whether it be writing code or writing a blog post, I struggle with it regularly.&lt;/p&gt;

&lt;p&gt;Do you feel the same? Which solution(s) work for you? Or do you have any other thoughts on this? I'm curious to read what you think of it.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>writing</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Getting Started with MongoDB - Building a Blog</title>
      <dc:creator>Rose</dc:creator>
      <pubDate>Sat, 11 Jul 2020 16:01:29 +0000</pubDate>
      <link>https://dev.to/shadowphoenix/getting-started-with-mongodb-building-a-blog-3771</link>
      <guid>https://dev.to/shadowphoenix/getting-started-with-mongodb-building-a-blog-3771</guid>
      <description>&lt;p&gt;Last time, I decided to use MongoDB for building my blog. As I have never worked with MongoDB before, I still have to figure out how to get everything to work. So, the only thing left to do now is getting started with MongoDB. How do we set it up, add users, and create our database.&lt;/p&gt;

&lt;p&gt;First, lets &lt;a href="https://www.mongodb.com/try/download/community"&gt;download&lt;/a&gt; the community server which is free and open source. I will be using the Windows version for my desktop and this article. After installing the software, I also installed &lt;a href="https://www.mongodb.com/products/shell"&gt;mongosh&lt;/a&gt; since I am an avid command line user. After adding mongosh to my PATH variables, I run powershell to see this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ccinfBnG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d46ng9w6g19okbl2jbmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ccinfBnG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d46ng9w6g19okbl2jbmz.png" alt="Getting Started with MongoDB in PowerShell using Mongosh"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a User
&lt;/h2&gt;

&lt;p&gt;Awesome! Time to figure out how to add a user. After all, I do not want everyone to simply log into my database. In order to do that, I first have to create an admin user and then enable access control. Figuring out how to do that was quite the task, since mongosh is still in beta an does not contain all functionality yet. So, to create a database with mongosh, we will have to use the following command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JG40NJ7d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xvn7x842jsw4pd5uom9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JG40NJ7d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xvn7x842jsw4pd5uom9m.png" alt="Adding a new Admin user to MongoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we should have a new user with admin rights. That allows us to shutdown the MongoDB server and enable access control. We can shutdown the server by using &lt;code&gt;db.adminCommand({shutdown:1})&lt;/code&gt;. After the server is down, we can take a look at the MongoDB config file to enable access control. You can find the file at &lt;code&gt;&amp;lt;installation directory&amp;gt;/bin/mongod.cfg&lt;/code&gt;. Here, we need to add the following lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Security:
  authorization: enabled
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is halfway when trying to boot the server again, that I figured that using mongosh might not have been the brightest of ideas. MongoDB is already new to me and using beta software that does not support all commands yet is not making things any easier. So, I switched to the legacy &lt;code&gt;mongo&lt;/code&gt; shell, since the documentation is up to date for that. I managed to boot the server again and logging into my database! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rWSbaUUo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sl3wz58m945ncuf31d6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rWSbaUUo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sl3wz58m945ncuf31d6v.png" alt="The Legacy Mongo, logged in."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Database
&lt;/h2&gt;

&lt;p&gt;I got my authentication ready, the final step now is to create a database. You can easily create a new database in MongoDB with the &lt;code&gt;use &amp;lt;database&amp;gt;&lt;/code&gt; command. If the database doesn't exist yet, it creates it. Keep in mind though that MongoDB will save the database only when you put data in it. Until then, it is not saved and does not show up when using the &lt;code&gt;show dbs&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;With MongoDB running on my desktop, I can finally start with a little proof of concept. I will use that proof of concept to figure out how to connect to MongoDB and add my first set of data using Spring Boot. That should allow me to get a better grip on the technologies to start implementing it for the actual project. You know... less hacky and a bit more clean. However, that is for another post!&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>learning</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
