{"id":158,"date":"2020-02-13T01:05:34","date_gmt":"2020-02-13T01:05:34","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=158"},"modified":"2020-02-14T11:48:57","modified_gmt":"2020-02-14T11:48:57","slug":"complete-mongodb-tutorial-step-by-step-demo-with-spring-boot","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/complete-mongodb-tutorial-step-by-step-demo-with-spring-boot\/","title":{"rendered":"Complete MongoDB Tutorial + Step by Step Demo with Spring Boot"},"content":{"rendered":"<p>In this demo, I would teach you how to set up and use MongoDB. It would be a complete MongoDB tutorial.<\/p>\n<p><a href=\"https:\/\/youtu.be\/11ZJq1_vrnE\" target=\"_blank\" rel=\"noopener noreferrer\">Watch the Video here<\/a><\/p>\n<p>You will learn the following:<\/p>\n<ol>\n<li><a href=\"#t1\">How to Download, Install and Start MongoDB<\/a><\/li>\n<li><a href=\"#t2\">Create Database, Collections(Tables) and Documents(Records)<\/a><\/li>\n<li><a href=\"#t3\">Setup and Use MongoDB GUI Application(Studio3T)<\/a><\/li>\n<li><a href=\"#t4\">Setup and Us MongoDB Compass(GUI Tool)<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Download, Install and Start MongoDB<\/strong><\/h5>\n<p>The first thing you should do is to download MongoDB. Get is from this link <a href=\"https:\/\/www.mongodb.com\/download-center\/community\">https:\/\/www.mongodb.com\/download-center\/community<\/a><\/p>\n<p>It downloads as a .msi installer file. Run the install to install MongoDB. Then restart your system<\/p>\n<p>Then you may go to your services.msc, find and stop MongoDB.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>To run MongoDB:<\/strong><\/p>\n<p>Open command prompt as an adminstrator<\/p>\n<p>Navigate to the MongoDB bin directory.<\/p>\n<p>Then run the following command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mongod<span style=\"color: #333333;\">.<\/span>exe <span style=\"color: #333333;\">--<\/span>dbpath<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"D:\\ProgramFiles\\MongoDB\\Server<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\4<\/span><span style=\"background-color: #fff0f0;\">.2\\data\"<\/span>\r\n<\/pre>\n<p>If the command runs successfully, then the server starts as shown below<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongDB-Server-Start.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-159 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongDB-Server-Start-1024x501.jpg\" alt=\"\" width=\"640\" height=\"313\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongDB-Server-Start-1024x501.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongDB-Server-Start-300x147.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongDB-Server-Start-768x376.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongDB-Server-Start.jpg 1240w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>Now the server have started, you can now minimize the window(Don&#8217;t Close it!)<\/p>\n<p>Next, open the bin directory and run the mongo.exe file. It opens the prompt where you can start entering and executing mongoDB commands.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_160\" aria-describedby=\"caption-attachment-160\" style=\"width: 640px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Client-Command-Prompt.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-160 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Client-Command-Prompt-1024x730.jpg\" alt=\"MongoDB Client Interface\" width=\"640\" height=\"456\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Client-Command-Prompt-1024x730.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Client-Command-Prompt-300x214.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Client-Command-Prompt-768x547.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Client-Command-Prompt.jpg 1099w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-160\" class=\"wp-caption-text\">MongoDB Client Interface<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s now get into the fun part!<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Create Database, Collections and Documents<\/strong><\/h5>\n<p>First, I would show you how to create these objects in the command line. It&#8217;s very easy. Then, we would go to download and install the GUI tool(Studio3T) and the use it.<\/p>\n<p>Just to be clear: in MongoDB terms, we use Database, Collections and Documents. Here, Collections corresponds to table while Document corresponds to Records. So we would actually insert a document into a collection.<\/p>\n<p>To view databases:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">show dbs\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Switch to a database:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">use test\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>View collection in the database:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">show collections\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>To create a collection and insert a document:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db.students.insert(\r\n  <span style=\"color: #557799;\">{<\/span>\r\n     <span style=\"color: #0000cc;\">name<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"kindson\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n     <span style=\"color: #0000cc;\">class<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #6600ee; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">,<\/span>\r\n     <span style=\"color: #0000cc;\">division<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"A\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n     <span style=\"color: #0000cc;\">subjects<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #333333;\">[<\/span><span style=\"background-color: #fff0f0;\">\"english\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"science\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"music\"<\/span><span style=\"color: #333333;\">]<\/span>\r\n  <span style=\"color: #557799;\">}<\/span>\r\n)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Update a collection, change the division to C:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db.students.update(\r\n  <span style=\"color: #557799;\">{<\/span> <span style=\"background-color: #fff0f0;\">\"name\"<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"kindson\"<\/span> <span style=\"color: #557799;\">}<\/span>,\r\n  <span style=\"color: #557799;\">{<\/span>\r\n    <span style=\"color: #996633;\">$set<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #333333;\">{<\/span> <span style=\"background-color: #fff0f0;\">\"division\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"C\"<\/span> <span style=\"color: #557799;\">}<\/span>\r\n  }\r\n)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Query a collection:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db.students.find()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Delete a document with the name, kindson:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db.students.remove( \r\n   <span style=\"color: #557799;\">{<\/span> <span style=\"background-color: #fff0f0;\">\"name\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"kindson\"<\/span> <span style=\"color: #557799;\">}<\/span> \r\n)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Drop a collection:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db.students.drop()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Setup the MongoDB GUI (Studio3T)<\/strong><\/h5>\n<p>You can download Studio3T from this link <a href=\"https:\/\/studio3t.com\/download\/\">https:\/\/studio3t.com\/download\/<\/a><\/p>\n<p><strong>Note<\/strong>: It was previously called RoboMongo<\/p>\n<p>After is successfully downloads, unzip it and you will see the executable file. Run it to install.<\/p>\n<p>Then we are all set. But keep in mind we have 30-day free trial. After which I recommend you pay for the licence which is just about $150 for the whole year.<\/p>\n<p>The user interface is shown below:<\/p>\n<figure id=\"attachment_162\" aria-describedby=\"caption-attachment-162\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Studio-3T-User-Interface.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-162 size-medium\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Studio-3T-User-Interface-300x192.jpg\" alt=\"Studio 3T User Interface\" width=\"300\" height=\"192\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Studio-3T-User-Interface-300x192.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Studio-3T-User-Interface-768x491.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Studio-3T-User-Interface-1024x655.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/Studio-3T-User-Interface.jpg 1087w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-162\" class=\"wp-caption-text\">Studio 3T User Interface<\/figcaption><\/figure>\n<h5><strong id=\"t4\">4. Get the MongoDB Compass<\/strong><\/h5>\n<p>I also recommend you download and install MongoDB Compass. This is another GUI tool used for managing MongoDB databases. With MongoDB Compass, you can perform CRUD operations(Create, Read, Update and Delete) in with your data. You will also be able to view and optimize your queries.<\/p>\n<p>Download <a href=\"https:\/\/www.mongodb.com\/download-center\/compass\" target=\"_blank\" rel=\"noopener noreferrer\">MongoDB Compass here<\/a><\/p>\n<figure id=\"attachment_166\" aria-describedby=\"caption-attachment-166\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Compass-GUI.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-166\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Compass-GUI-300x177.jpg\" alt=\"MongoDB Compass GUI\" width=\"300\" height=\"177\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Compass-GUI-300x177.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Compass-GUI-768x453.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Compass-GUI-1024x603.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2020\/02\/MongoDB-Compass-GUI.jpg 1149w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-166\" class=\"wp-caption-text\">MongoDB Compass GUI<\/figcaption><\/figure>\n<p><strong>Next Steps<\/strong><\/p>\n<p>As for how to use the GUI to create and manage databases, I recommend you watch the detailed video lessons. In the next class, we would the create a project in Spring Boot that would store data in MongoDB.<\/p>\n<p><strong>Watch the Video below<\/strong><\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/11ZJq1_vrnE\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this demo, I would teach you how to set up and use MongoDB. It would be a complete MongoDB tutorial. Watch the Video here &hellip; <\/p>\n","protected":false},"author":1,"featured_media":163,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,1],"tags":[52,53,54],"class_list":["post-158","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","category-uncategorized","tag-mongodb-compass","tag-nosql","tag-unstructured-data"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":6,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/158\/revisions\/169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/163"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}