{"id":873,"date":"2018-11-05T10:25:23","date_gmt":"2018-11-05T10:25:23","guid":{"rendered":"https:\/\/sites.alldaycity.com\/mirageglobe\/?p=873"},"modified":"2018-11-09T10:19:24","modified_gmt":"2018-11-09T10:19:24","slug":"mongodb-guide-to-basics","status":"publish","type":"post","link":"https:\/\/pages.alldaycity.com\/mirageglobe\/mongodb-guide-to-basics\/","title":{"rendered":"Mongodb guide to basics"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1335\" src=\"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-content\/uploads\/sites\/2\/2018\/11\/bandwidth-close-up-computer-1148820.jpg\" alt=\"\" class=\"wp-image-883\" srcset=\"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-content\/uploads\/sites\/2\/2018\/11\/bandwidth-close-up-computer-1148820.jpg 2000w, https:\/\/pages.alldaycity.com\/mirageglobe\/wp-content\/uploads\/sites\/2\/2018\/11\/bandwidth-close-up-computer-1148820-300x200.jpg 300w, https:\/\/pages.alldaycity.com\/mirageglobe\/wp-content\/uploads\/sites\/2\/2018\/11\/bandwidth-close-up-computer-1148820-768x513.jpg 768w, https:\/\/pages.alldaycity.com\/mirageglobe\/wp-content\/uploads\/sites\/2\/2018\/11\/bandwidth-close-up-computer-1148820-1024x684.jpg 1024w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><figcaption>database by pexels<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">mongodb is a document type database aka NoSQL. The power of NoSQL DBs are its flexibility of unfixed schemas unlike in mysql\/oracle SQL based database. A schema enforces fixed columns; thus columns will need to be defined before the database can be used. NoSQL type on the other hand allows key:value type of definition in each document (just like records), and these can differ from other documents in the same set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, are references to basic mongodb commands that should get you started.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">installing mongodb<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">on macs use homebrew (https:\/\/brew.sh\/) if you do not have it; get it. once installed, you can view the list of brew packages via the following command in terminal:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ brew search [yourpackage]<br>$ brew search mongo<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">now you can install mongodb via<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ brew install mongodb<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">on linux (debian or ubuntu) use the following: (reference :https:\/\/docs.mongodb.com\/manual\/tutorial\/install-mongodb-on-ubuntu\/ )<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$&nbsp;sudo apt-key adv &#8211;keyserver hkp:\/\/keyserver.ubuntu.com:80 &#8211;recv 9DA31620334BD75D9DCB49F368818C72E52529D4<br>$&nbsp;echo &#8220;deb [ arch=amd64,arm64 ] https:\/\/repo.mongodb.org\/apt\/ubuntu xenial\/mongodb-org\/4.0 multiverse&#8221; | sudo tee \/etc\/apt\/sources.list.d\/mongodb-org-4.0.list<br>$ sudo apt-get update<br>$&nbsp;sudo apt-get install -y mongodb-org<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">you are highly encouraged to refer to the above url (of mongodb community) to check the keys as well as the version of your OS so that you can install the appropriate distribution package intended for.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">adding root user<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If no users are configured in admin.system.users, one may access the database from the localhost interface without authenticating. Thus, from the server running the database (and thus on localhost), run the database shell and configure an administrative user:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ mongo<br>$ use admin<br>$ db.createUser( { user: &#8220;mongoroot&#8221;, pwd: &#8220;mysuperdupermongorooterpassword&#8221;, roles: [ { role: &#8220;root&#8221; , db: &#8220;admin&#8221; } ] } )<br>$ db.createUser( { user: &#8220;mongoadmin&#8221;, pwd: &#8220;mysuperdupermongorooterpassword&#8221;, roles: [ { role: &#8220;userAdminAnyDatabase&#8221;, db: &#8220;admin&#8221; } ] } )<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">now remove admin user<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ use admin<br>$ db.dropUser(&#8220;mongoadmin&#8221;)<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">turn on authentication by editing mongo configuration file. go to approximately line 28 and remove the # from auth=true. if you are not familar with vim, nano is just as good.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ vim \/etc\/mongod.conf<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">now relaunch mongodb<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ service mongod restart<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">test logging in by using the mongo command with parsing password<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ mongo &#8211;username mongoroot &#8211;password &#8211;authenticationDatabase admin<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">setup single db admin<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ use mynewdb<br>$ db.createUser( { user: &#8220;accountUser&#8221;, pwd: &#8220;password&#8221;, roles: [ &#8220;readWrite&#8221;, &#8220;dbAdmin&#8221; ] } )<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">ref: http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.createUser\/<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">mongodb default location<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>configuration: \/etc\/mongodb.conf<\/li><li>database: \/var\/lib\/mongodb<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">if you install via homebrew on mac, this will be different<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">mongodb basic commands<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">list all databases<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ show dbs<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">select a database<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ use mynewdb<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">show current db<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ db<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">add user<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ use mynewdb<br>$ db.createUser( { user: &#8220;mynewadmin&#8221;, pwd: &#8220;passwordchangeme&#8221;, roles: [ &#8220;readWrite&#8221;, &#8220;dbAdmin&#8221; ] } )<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">change password<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ db.changePassword(&#8220;mynewadmin&#8221;, &#8220;yetanothernewpassword&#8221;)<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">drop user<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ db.dropUser(&#8220;mynewadmin&#8221;)<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">add master admin<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ use admin<br>$ db.createUser( { user: &#8220;mongoroot&#8221;, pwd: &#8220;mysuperdupermongorooterpassword&#8221;, roles: [ { role: &#8220;root&#8221; , db: &#8220;admin&#8221; } ] } )<br>$ db.createUser( { user: &#8220;mongoadmin&#8221;, pwd: &#8220;mysuperdupermongoadminpassword&#8221;, roles: [ { role: &#8220;userAdminAnyDatabase&#8221;, db: &#8220;admin&#8221; } ] } )<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">restarting mongo<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ service mongod restart<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">a sample setup mongodb<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>user: mongoprodroot<\/li><li>user: mongoprodadmin<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$ db.createUser( { user: &#8220;mongoprodroot&#8221;, pwd: &#8220;yetanotheruniquepassword&#8221;, roles: [ { role: &#8220;root&#8221; , db: &#8220;admin&#8221; } ] } )<br>$ db.createUser( { user: &#8220;mongoprodadmin&#8221;, pwd: &#8220;yetanotheruniquepassword&#8221;, roles: [ { role: &#8220;userAdminAnyDatabase&#8221;, db: &#8220;admin&#8221; } ] } )<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">vultr referral link<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">if you are setting up a cloud server, my referral link banner is below<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.vultr.com\/?ref=6877905\"><img decoding=\"async\" src=\"https:\/\/www.vultr.com\/media\/banner_2.png\" alt=\"\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>mongodb is a document type database aka NoSQL. The power of NoSQL DBs are its flexibility of unfixed schemas unlike in mysql\/oracle SQL based database. A schema enforces fixed columns; thus columns will need to be defined before the database can be used. NoSQL type on the other hand allows key:value type of definition in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-873","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/posts\/873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/comments?post=873"}],"version-history":[{"count":13,"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/posts\/873\/revisions"}],"predecessor-version":[{"id":906,"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/posts\/873\/revisions\/906"}],"wp:attachment":[{"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/media?parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/categories?post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pages.alldaycity.com\/mirageglobe\/wp-json\/wp\/v2\/tags?post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}