{"id":16483,"date":"2013-02-19T21:01:07","date_gmt":"2013-02-19T13:01:07","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=16483"},"modified":"2020-03-16T16:36:17","modified_gmt":"2020-03-16T08:36:17","slug":"webdev-with-mongodb-part2","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/","title":{"rendered":"MongoDB For Beginners &#8211; Basic Shell Commands (Part II)"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part1\/\">previous MongoDB\u2019s guide for beginner\u2019s<\/a>, I\u2019ve covered the importance and terminology of MongoDB as well as how to <a href=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part1\/#mongo_win\">setup Mongo on Windows<\/a> and <a href=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part1\/#mongo_mac\">Mac<\/a>. Resuming where we left off, today we\u2019re going to look into some basic and usefull shell commands for MongoDB.<\/p>\n<p>You can perform almost any action through the Mongo shell. Since this is a beginner\u2019s guide I won\u2019t delve very far down into this rabbit hole. But you better believe there is <a href=\"https:\/\/docs.mongodb.com\/manual\/mongo\/\">plenty of documentation<\/a> on the topic to research further.<\/p>\n<div class=\"ref-block ref-block--post\" id=\"ref-post-1\">\n\t\t\t\t\t<a href=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part1\/\" class=\"ref-block__link\" title=\"Read More: MongoDB For Beginners \u2013 Introduction and Installation (Part I)\" rel=\"bookmark\"><span class=\"screen-reader-text\">MongoDB For Beginners \u2013 Introduction and Installation (Part I)<\/span><\/a>\n<div class=\"ref-block__thumbnail img-thumb img-thumb--jumbo\" data-img='{ \"src\" : \"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/webdev-with-mongodb-part1.jpg\" }'>\n\t\t\t\t\t\t\t<noscript>\n<style>.no-js #ref-block-post-16425 .ref-block__thumbnail { background-image: url(\"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/webdev-with-mongodb-part1.jpg\"); }<\/style>\n<\/noscript>\n\t\t\t\t\t\t<\/div>\n<div class=\"ref-block__summary\">\n<h4 class=\"ref-title\">MongoDB For Beginners \u2013 Introduction and Installation (Part I)<\/h4>\n<p class=\"ref-description\">\n\t\t\t\t\t\tLearn how to use MongoDB for web development in this comprehensive guide. Part 1 covers the basics to...\t\t\t\t\t\t<span>Read more<\/span><\/p>\n<\/div>\n<\/div>\n<p>Let\u2019s use this small segment to create a collection of TV Shows which we can then reference later using PHP. If you run the command above, you\u2019ll notice there are only two databases installed by default.<\/p>\n<pre>&gt; show dbs<\/pre>\n<p>We can use the test database and inside create a new collection named \u201cshows\u201d which will hold our TV Show documents. So first I will define a couple of variables inside the shell window.<\/p>\n<p>I\u2019m using three different TV Shows along with their original debut airdate and the television network they ran under.<\/p>\n<p>We need to use the MongoDB <a href=\"https:\/\/docs.mongodb.com\/manual\/tutorial\/update-documents\/#Updating-%7B%7Bsave%28%29%7D%7Dinthemongoshell\">.save()<\/a> command for saving new data and creating new collections.<\/p>\n<pre>&gt; a = { title:\"Arrested Development\", airdate:\"November 2, 2003\", network:\"FOX\" }\r\n&gt; b = { title:\"Stella\", airdate:\"June 28, 2005\", network:\"Comedy Central\" } \r\n&gt; c = { title:\"Modern Family\", airdate:\"September 23, 2009\", network:\"ABC\" }\r\n<\/pre>\n<p>If you enter each of these lines into the Mongo shell terminal you\u2019ll get a response back with the JSON data formatted. We have just setup 3 variables which can now be passed into the save command for storing each TV Show as a document object in our collection of shows.<\/p>\n<p>If you notice we haven\u2019t actually created any new collection named shows. This is done on-the-fly by MongoDB only after you add some data into a collection. We access and create collections using standard JavaScript dot syntax.<\/p>\n<p>Copy and run the codes below:<\/p>\n<pre>&gt; db.shows.save(a)\r\n&gt; db.shows.save(b)\r\n&gt; db.shows.save(c)\r\n<\/pre>\n<p>This will add each JSON object variable we created into the shows collection. After running the first command on TV Show A we will have a new collection displaying inside our test db. You can verify this by running <code>show collections<\/code> in the terminal.<\/p>\n<p>But even more interesting, we can check if all the data is saved properly by running the <code>find()<\/code> query command as below:<\/p>\n<pre>&gt; db.shows.find()\r\n<\/pre>\n<p>In the next article, we\u2019ll take a look into <strong>how to setup MongoDB for PHP<\/strong><\/p>\n<div class=\"ref-block ref-block--post\" id=\"ref-post-2\">\n\t\t\t\t\t<a href=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part3\/\" class=\"ref-block__link\" title=\"Read More: MongoDB For Beginners \u2013 Setting Up MongoDB For PHP (Part III)\" rel=\"bookmark\"><span class=\"screen-reader-text\">MongoDB For Beginners \u2013 Setting Up MongoDB For PHP (Part III)<\/span><\/a>\n<div class=\"ref-block__thumbnail img-thumb img-thumb--jumbo\" data-img='{ \"src\" : \"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/webdev-with-mongodb-part3.jpg\" }'>\n\t\t\t\t\t\t\t<noscript>\n<style>.no-js #ref-block-post-16530 .ref-block__thumbnail { background-image: url(\"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/webdev-with-mongodb-part3.jpg\"); }<\/style>\n<\/noscript>\n\t\t\t\t\t\t<\/div>\n<div class=\"ref-block__summary\">\n<h4 class=\"ref-title\">MongoDB For Beginners \u2013 Setting Up MongoDB For PHP (Part III)<\/h4>\n<p class=\"ref-description\">\n\t\t\t\t\t\tBefore diving into this, I recommend that you read the following related articles if you have not: Introduction...\t\t\t\t\t\t<span>Read more<\/span><\/p>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In the previous MongoDB\u2019s guide for beginner\u2019s, I\u2019ve covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac. Resuming where we left off, today we\u2019re going to look into some basic and usefull shell commands for MongoDB. You can perform almost any action through the Mongo shell.&hellip;<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3392],"tags":[2315],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>MongoDB For Beginners: Basic Shell Commands<\/title>\n<meta name=\"description\" content=\"In the previous MongoDB&#039;s guide for beginner&#039;s, I&#039;ve covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB For Beginners - Basic Shell Commands (Part II)\" \/>\n<meta property=\"og:description\" content=\"In the previous MongoDB&#039;s guide for beginner&#039;s, I&#039;ve covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2013-02-19T13:01:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-16T08:36:17+00:00\" \/>\n<meta name=\"author\" content=\"Jake Rocheleau\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jake Rocheleau\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/\"},\"author\":{\"name\":\"Jake Rocheleau\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/966b2daea15283b4145e71aa98a82c2a\"},\"headline\":\"MongoDB For Beginners &#8211; Basic Shell Commands (Part II)\",\"datePublished\":\"2013-02-19T13:01:07+00:00\",\"dateModified\":\"2020-03-16T08:36:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/\"},\"wordCount\":393,\"commentCount\":8,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"MongoDB\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/\",\"name\":\"MongoDB For Beginners: Basic Shell Commands\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2013-02-19T13:01:07+00:00\",\"dateModified\":\"2020-03-16T08:36:17+00:00\",\"description\":\"In the previous MongoDB's guide for beginner's, I've covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/webdev-with-mongodb-part2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MongoDB For Beginners &#8211; Basic Shell Commands (Part II)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/966b2daea15283b4145e71aa98a82c2a\",\"name\":\"Jake Rocheleau\",\"description\":\"Jake is a writer and designer with over 10 years experience working on the web. He writes about user experience design and cool resources for designers\",\"sameAs\":[\"https:\\\/\\\/www.hongkiat.com\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/jake\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"MongoDB For Beginners: Basic Shell Commands","description":"In the previous MongoDB's guide for beginner's, I've covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB For Beginners - Basic Shell Commands (Part II)","og_description":"In the previous MongoDB's guide for beginner's, I've covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac.","og_url":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2013-02-19T13:01:07+00:00","article_modified_time":"2020-03-16T08:36:17+00:00","author":"Jake Rocheleau","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Jake Rocheleau","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/"},"author":{"name":"Jake Rocheleau","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/966b2daea15283b4145e71aa98a82c2a"},"headline":"MongoDB For Beginners &#8211; Basic Shell Commands (Part II)","datePublished":"2013-02-19T13:01:07+00:00","dateModified":"2020-03-16T08:36:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/"},"wordCount":393,"commentCount":8,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["MongoDB"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/","url":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/","name":"MongoDB For Beginners: Basic Shell Commands","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2013-02-19T13:01:07+00:00","dateModified":"2020-03-16T08:36:17+00:00","description":"In the previous MongoDB's guide for beginner's, I've covered the importance and terminology of MongoDB as well as how to setup Mongo on Windows and Mac.","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/webdev-with-mongodb-part2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"MongoDB For Beginners &#8211; Basic Shell Commands (Part II)"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/966b2daea15283b4145e71aa98a82c2a","name":"Jake Rocheleau","description":"Jake is a writer and designer with over 10 years experience working on the web. He writes about user experience design and cool resources for designers","sameAs":["https:\/\/www.hongkiat.com"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/jake\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-4hR","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/16483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=16483"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/16483\/revisions"}],"predecessor-version":[{"id":49627,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/16483\/revisions\/49627"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=16483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=16483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=16483"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=16483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}