{"id":1428,"date":"2018-10-23T04:13:42","date_gmt":"2018-10-23T04:13:42","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1428"},"modified":"2018-10-24T02:30:59","modified_gmt":"2018-10-24T02:30:59","slug":"mongodb-basics-create-show-and-drop-collections","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/mongodb-basics-create-show-and-drop-collections\/","title":{"rendered":"MongoDB basics &#8212; Create, Show and Drop Collections"},"content":{"rendered":"<h1 class=\"entry-title\">MongoDB basics \u2014 Create, Show and Drop Collections<\/h1>\n<p>MongoDB is a NoSQL database. This means that unlike relational databases there\u2019s no set in stone schema with various row and columns or fields with well-defined data types. Analogue to traditional SQL database tables, MongoDB has collections. Where there were once rows in a SQL table, MongoDB has documents. Data is queried across all collections and documents using key-value pairs, which you will soon see.<\/p>\n<p>The utility of MongoDB is its ease of use, scalability and the JSON like syntax with which the stored data is represented. On the other hand, if you wish to do crazy operations on your datasets like JOINs you may find MongoDB cumbersome and traditional SQL databases are better suited for that.<\/p>\n<div class=\"zvrT4DKI\"><\/div>\n<p>In any case, this article would not presume any familiarity with databases whatsoever. We will just assume that you have MongoDB installed on your server\/desktop (it is available on Windows, Mac and Linux). With that installed we will create our sample database and see MongoDB in action.<\/p>\n<div class=\"q2Zr4UOQ\"><\/div>\n<h1>Prerequisites<\/h1>\n<ol>\n<li>MongoDB installation. You can follow the\u00a0<a href=\"https:\/\/www.mongodb.com\/download-center?initial=true#community\">official documentation<\/a>\u00a0to install your current Operating System. OR<\/li>\n<li>Optionally, you can sign up for\u00a0<a href=\"https:\/\/www.mongodb.com\/cloud\/atlas\">MongoDB atlas<\/a>. They offer a free tier with 512MB of persistent storage. Perfect environment for experimentation or small projects.<\/li>\n<li>If you wish to not install any software whatsoever, you can visit Katacoda and use their web-based interface as an ephemeral sandboxed environment.<\/li>\n<\/ol>\n<h2>Getting Started<\/h2>\n<p>Assuming you have MongoDB server installed and a shell connected to the server we can start exploring a few features of it. But first a few terminologies \u2014 A mongodb server has a list of\u00a0<strong><em>databases\u00a0<\/em><\/strong>dbs in it. Each database can have multiple\u00a0<strong><em>collections\u00a0<\/em><\/strong>in it.<\/p>\n<p>So for example, a University can have a personnel database which can then have various collections for different departments like one collection for Mathematics, one for Biology and so on.<span id=\"ezoic-pub-ad-placeholder-135\" class=\"ezoic-adpicker-ad\"><\/span><span id=\"div-gpt-ad-linuxhint_com-medrectangle-4-0\" class=\"ezoic-ad ezfound\" data-google-query-id=\"COHjldqDnt4CFcjnZAodrVwARw\"><\/span><\/p>\n<div id=\"google_ads_iframe_\/1254144\/linuxhint_com-medrectangle-4_0__container__\">\n<p>Each collection can then have a document inside it, a document would have an individual staff personnel\u2019s details listed in it. As mentioned before, the stored data is represented in a JSON-like fashion and we can query different values using the keys they are paired with.<\/p>\n<h3>Create Database<\/h3>\n<p>Creating a database happens implicitly when you try to use a database. In this example, if you are in Mongo shell and you type in:<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=USE\"><span class=\"kw1\">use<\/span><\/a>\u00a0testDb<\/div>\n<\/div>\n<p>MongoDB first checks to see if you have a database with the name testdb, if not, then it creates a new one for you to use and the Mongo Shell\u00a0<strong><em>switches\u00a0<\/em><\/strong>to testdb. This means that every collection and document created, updated or read would be from this database, unless explicitly specified otherwise.<\/p>\n<p>You can use the command &gt; db to print what database you are in right now and use the command &gt; show dbs\u00a0 to list all of the databases available and created.<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0db<br \/>\ntestDb<br \/>\n<span class=\"sy1\">&gt;<\/span>\u00a0<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=SHOW\"><span class=\"kw1\">show<\/span><\/a>\u00a0dbs<br \/>\nadmin\u00a0\u00a0 0.000GB<br \/>\nconfig\u00a0 0.000GB<br \/>\n<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=LOCAL\"><span class=\"kw1\">local<\/span><\/a>\u00a0\u00a0 0.000GB<br \/>\nmydb\u00a0\u00a0\u00a0 0.000GB<\/div>\n<\/div>\n<p>You may want to leave the admin, config databases as it is used by Mongo for administrative purposes.<\/p>\n<h3>Create Collection<span id=\"ezoic-pub-ad-placeholder-141\" class=\"ezoic-adpicker-ad\"><\/span><\/h3>\n<p>To create a collection, first ensure that you are in the appropriate database where you intend on creating the collection. You can now create a collection in two different way:<\/p>\n<h4>1.\u00a0\u00a0 Explicitly Creating a Collection:<\/h4>\n<p>Using the command:<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0db.createCollection<span class=\"br0\">(<\/span><span class=\"st0\">&#8220;testCollection1&#8221;<\/span><span class=\"br0\">)<\/span><span class=\"sy2\">;<\/span><br \/>\n<span class=\"br0\">{<\/span>\u00a0<span class=\"st0\">&#8220;ok&#8221;<\/span>\u00a0:\u00a0<span class=\"nu0\">1<\/span>\u00a0<span class=\"br0\">}<\/span><\/div>\n<\/div>\n<p>This created a collection named testCollection1.<\/p>\n<h4>2.\u00a0\u00a0 Inserting a Document to a new collection<\/h4>\n<p>Alternatively, you can easily try to insert a document in a collection that doesn\u2019t exist. Mongo will create a collection for you. Please note that while this is a convenience in terms of programmatically creating collections, if you are using Mongo shell and make a typo somewhere while trying to insert a document, the document may end up in a new database unbeknownst to you.<br \/>\nThe syntax is: db.collection_name.insert(document);<br \/>\nHere db is\u00a0<strong>literally\u00a0<\/strong>the string db, collection<\/p>\n<p>For example, to create a collection testCollection2 in testDb database use the following command:<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0db.testCollection2.<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/string-functions.html\"><span class=\"kw14\">insert<\/span><\/a><span class=\"br0\">(<\/span><span class=\"br0\">{<\/span><br \/>\nname:\u00a0<span class=\"st0\">&#8220;John&#8221;<\/span><span class=\"sy2\">,<\/span><br \/>\n<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=KEY\"><span class=\"kw1\">key<\/span><\/a>: \u201c<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=VALUE\"><span class=\"kw1\">value<\/span><\/a>\u201d<br \/>\nage:\u00a0<span class=\"nu0\">25<\/span><br \/>\n<span class=\"br0\">}<\/span><span class=\"br0\">)<\/span><span class=\"sy2\">;<\/span><\/div>\n<\/div>\n<p>Here, the document part is represented my the following JSON string:<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"br0\">{<\/span><br \/>\nname:\u00a0<span class=\"st0\">&#8220;John&#8221;<\/span><span class=\"sy2\">,<\/span><br \/>\n<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=KEY\"><span class=\"kw1\">key<\/span><\/a>: \u201c<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=VALUE\"><span class=\"kw1\">value<\/span><\/a>\u201d<br \/>\nage:\u00a0<span class=\"nu0\">25<\/span><br \/>\n<span class=\"br0\">}<\/span><\/div>\n<\/div>\n<p>These are the key-value pairs typical of a JSON string. The name is key and \u201cJohn\u201d is value. You can have multiple documents in this collection with the key name and a different value for name, say, Jane.<\/p>\n<p>To list all the collections inside a given database, use the command:<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=SHOW\"><span class=\"kw1\">show<\/span><\/a>\u00a0collections<br \/>\ntestCollection1<br \/>\ntestCollection2<\/div>\n<\/div>\n<p>You can see both the collections are now created. We have also inadvertently learned how to add a new document to a collection.<\/p>\n<h4>Show<\/h4>\n<p>We have been using show keyword quite a lot to list the collections and databases. Just to recap this a bit, these were the commands:<span id=\"ezoic-pub-ad-placeholder-137\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=SHOW\"><span class=\"kw1\">show<\/span><\/a>\u00a0dbs<br \/>\n<span class=\"sy1\">&gt;<\/span>\u00a0<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=SHOW\"><span class=\"kw1\">show<\/span><\/a>\u00a0collections<\/div>\n<\/div>\n<p>These along with the command db to print the current database can come in quite handy while interacting with the Mongo shell.<\/p>\n<h3>Drop Collections and Drop Databses<\/h3>\n<p>The keyword drop is something we haven\u2019t come across so far. It is used to remove collections or even entire databases from your mongo server. The following syntax walks you through the process:<\/p>\n<h4>1.\u00a0 Dropping Collection<\/h4>\n<p>Let\u2019s get rid of the collection testCollection2 we created earilier:<\/p>\n<div class=\"codecolorer-container mysql default\">\n<div class=\"mysql codecolorer\"><span class=\"sy1\">&gt;<\/span>\u00a0db.testCollection2.<a href=\"http:\/\/search.oracle.com\/search\/search?group=MySQL&amp;q=DROP\"><span class=\"kw1\">drop<\/span><\/a><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>You can use the show collections command to verify that this indeed worked. There will be one database less than we had previously, I will let you guess which one will be missing.<\/p>\n<h4>2.\u00a0 Drop Database<\/h4>\n<p>Before you blindly run the command to drop the database,\u00a0<strong>make absolutely sure that you are in the right<\/strong>\u00a0<strong>database<\/strong>. Or else you might end up losing valuable data stored elsewhere. We will be dropping the database testDb that we created earlier, let\u2019s make sure that that\u2019s where we are:<span id=\"ezoic-pub-ad-placeholder-159\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"codecolorer-container bash default\">\n<div class=\"bash codecolorer\"><span class=\"sy0\">&gt;<\/span>\u00a0db<br \/>\ntestDb<br \/>\n<span class=\"sy0\">&gt;<\/span>\u00a0db.dropDatabase<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>;<\/div>\n<\/div>\n<p>The latter command drops the database, as you can tell from the name.<\/p>\n<h4>Conclusion<\/h4>\n<p>MongoDB has gain popularity along with the Node.js project. They both share a kind of symbiosis which enabled each to be a success. JSON like representation, scalability and an ease and dynamic way of creating documents has earned MongoDB quite the fame.<\/p>\n<p>If you are looking for database technology for a quick weekend project or even for some serious data heavy-lifting, MongoDB is an option you should give some serious consideration.<\/p>\n<\/div>\n<p><a href=\"https:\/\/linuxhint.com\/mongodb_create_drop_collections\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB basics \u2014 Create, Show and Drop Collections MongoDB is a NoSQL database. This means that unlike relational databases there\u2019s no set in stone schema with various row and columns or fields with well-defined data types. Analogue to traditional SQL database tables, MongoDB has collections. Where there were once rows in a SQL table, MongoDB &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/mongodb-basics-create-show-and-drop-collections\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MongoDB basics &#8212; Create, Show and Drop Collections&#8221;<\/span><\/a><\/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-1428","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1428"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1428\/revisions"}],"predecessor-version":[{"id":1490,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1428\/revisions\/1490"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}