{"id":311,"date":"2017-04-02T02:00:38","date_gmt":"2017-04-02T02:00:38","guid":{"rendered":"http:\/\/kaizen-koka.com\/?p=311"},"modified":"2017-04-02T03:05:51","modified_gmt":"2017-04-02T03:05:51","slug":"mongodb-nosql","status":"publish","type":"post","link":"https:\/\/kaizen-koka.com\/?p=311","title":{"rendered":"MongoDB &#8211; NoSQL"},"content":{"rendered":"<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>&#8211;<\/strong> Its document-oriented NoSQL database by 10gen.<br \/>\n\t<strong>&#8211; <\/strong>It is scalable, open source, high-performance document oriented DB.<br \/>\n\t<strong>&#8211; <\/strong>New nodes can be added to NoSQL databases. Which means we can achieve horizontal scalability (linearly proposal to performance. More DB&#39;s -&gt; more performance)<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\">NoSQL database types:<\/span><\/span>\n<\/p>\n<p>\n\t<a href=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"312\" data-permalink=\"https:\/\/kaizen-koka.com\/?attachment_id=312\" data-orig-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?fit=926%2C313&amp;ssl=1\" data-orig-size=\"926,313\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screen Shot 2017-04-01 at 8.58.50 PM\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?fit=300%2C101&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?fit=926%2C313&amp;ssl=1\" alt=\"Screen Shot 2017-04-01 at 8.58.50 PM\" class=\"aligncenter size-full wp-image-312\" height=\"313\" src=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?resize=926%2C313\" width=\"926\" srcset=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?w=926&amp;ssl=1 926w, https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?resize=300%2C101&amp;ssl=1 300w, https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-01-at-8.58.50-PM.png?resize=768%2C260&amp;ssl=1 768w\" sizes=\"auto, (max-width: 926px) 100vw, 926px\" \/><\/a>\n<\/p>\n<p>\n\t<br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>Pros<\/strong>:<br \/>\n\t&#8211; Query language<br \/>\n\t&#8211; Fast performance<br \/>\n\t&#8211; Horizontal scalability<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>Cons<\/strong>:<br \/>\n\t&#8211; No join support<br \/>\n\t&#8211; No complex transactions support<br \/>\n\t&#8211; No constraints support<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>&#8211;<\/strong> Entity can be represented by collections(table)<br \/>\n\t<strong>&#8211; <\/strong>Its stores documents(objects, row) with in a collection(table) in BSON format<br \/>\n\t&nbsp; &nbsp;var mydoc = {<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_id: ObjectId(&quot;5099803df3f4948bd2f98391&quot;), &nbsp;&lt;- mandatory<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name: { first: &quot;Alan&quot;, last: &quot;Turing&quot; },<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;birth: new Date(&#39;Jun 23, 1912&#39;),<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;death: new Date(&#39;Jun 07, 1954&#39;),<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contribs: [ &#8220;Turing machine&#8221;, &#8220;Turing test&#8221;, &#8220;Turingery&#8221; ],<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;views : NumberLong(1250000)<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n\t&nbsp; &nbsp;The above fields have the following data types:<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\">&nbsp;&nbsp; &nbsp;_id holds an ObjectId.<br \/>\n\t&nbsp;&nbsp; &nbsp;name holds an embedded document that contains the fields first and last.<br \/>\n\t&nbsp;&nbsp; &nbsp;birth and death hold values of the Date type.<br \/>\n\t&nbsp;&nbsp; &nbsp;contribs holds an array of strings.<br \/>\n\t&nbsp;&nbsp; &nbsp;views holds a value of the NumberLong type. &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>Document Oriented Query Language<\/strong>: Wild cards can be used.<br \/>\n\t&nbsp; db.employess.find({_id:1234});<br \/>\n\t&nbsp; db.employees.find({name:&quot;Hasan&quot;});<br \/>\n\t&nbsp; db.employees.find().sort({name:1});<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\">&#8211; <strong>Adhoc Queries<\/strong>: Support search by field, range queries, and regular expression searches.<br \/>\n\t&#8211; <strong>Indexing support<\/strong> : Any field in a document can be indexed.<br \/>\n\t&#8211; <strong>Replication support<\/strong>: supports master-slave replication<br \/>\n\t&nbsp; &nbsp;&#8211; A master can perform reads and writes.<br \/>\n\t&nbsp; &nbsp;&#8211; A slave can perform master and can only be used for reads and backup.<br \/>\n\t&#8211; <strong>Duplication data<\/strong>: Runs over multiple servers. The data is duplicated to keep the system running on hardware failures.<br \/>\n\t&#8211; <strong>Load balancing<\/strong>: (Automatic)<br \/>\n\t&#8211; <strong>Scalable<\/strong>: New nodes can be provisioned against running DB. Scales horizontally.<br \/>\n\t&#8211; <strong>Capped collection<\/strong>: circular queue.<br \/>\n\t&#8211; <strong>File storage<\/strong>: MongoDB can be used as a file system, taking advantage of load balancing &amp; data replication features. Special feature GridFS available for this purpose.<br \/>\n\t&#8211; <strong>Aggregation<\/strong>: MapReduce can be used for batch processing of data and aggregation operations. Offers similar functionalities as SQL Group by clause.<br \/>\n\t&#8211;<strong> Server side javascript execution<\/strong>: java script can be used in queries.<br \/>\n\t&#8211; Location support<br \/>\n\t&#8211; No conversion required between OOPL &amp; MongoDB.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>Basic Queries<\/strong>: &nbsp;(RoboMOngo -&gt; Groovy Adminstrative UI)<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\">1. create the database -&gt; use cricketleague<br \/>\n\t2. create a table(collections)-&gt; db.createCollection(&quot;Teams&quot;);<br \/>\n\t3. list a collection(table) with in DB -&gt; show collections<br \/>\n\t4. drop a collection -&gt; db.Teams.drop();<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\">5. insert data into collection -&gt;<br \/>\n\t&nbsp; &nbsp; a={&quot;name&quot;:&quot;siva&quot;, &quot;conference&quot;:&quot;America&quot;}<br \/>\n\t&nbsp; &nbsp; db.Teams.save(a);<br \/>\n\t6. query data from collection -&gt;<br \/>\n\t&nbsp; &nbsp; db.Teams.find(&quot;name&quot;:&quot;NY&quot;);<br \/>\n\t7. multiple update a document(row) with in a collection -&gt;<br \/>\n\t&nbsp; &nbsp; db.Teams.update({&quot;name&quot;:&quot;NY&quot; },{&quot;name&quot;:&quot;XY&quot;},{multi:true});<br \/>\n\t8. single update a document(row) with in a collection -&gt;<br \/>\n\t&nbsp; &nbsp; db.Teams.update({_id:1010,&quot;name&quot;:&quot;NY&quot;});<br \/>\n\t&nbsp; &nbsp; db.Teams.remove({_id:1001});<br \/>\n\t9. Bulk load or script data in to the collection<br \/>\n\t&nbsp; &nbsp; load(&lt;&quot;file path&quot;&gt;);<br \/>\n\t10. db.CopyDatabase(&quot;cricketLeague&quot;, &quot;Football League&quot;, &quot;localhost&quot;);<br \/>\n\t11. Aggregator:<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;db.users.aggregate({<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $group:{<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _id:&quot;eyecolor&quot;,<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total:{$sum:1}<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;});<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;db.users.aggregate({<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $group:{<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_id:&quot;$gender&quot;,<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;avgAge:{$avg:&quot;$age&quot;}<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n\t&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;})<br \/>\n\t12. db.users.ensureIndex({&quot;age&quot;:1})<br \/>\n\t13. db.users.getIndexes()<\/span><\/span>\n<\/p>\n<p>\n\t<br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\"><strong>Spring MongoDB integration<\/strong>:<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:tahoma,geneva,sans-serif;\">.MongoDBFactory<br \/>\n\t.SimpleMongoDBFactory<br \/>\n\t.MongoOperations<\/span><\/span>\n<\/p>\n<pre class=\"brush:java;\">\r\n@Bean\r\npublic MongoDbFactory mongoDbFactory(){\r\n&nbsp;&nbsp; &nbsp;return new MongoClient(MONGO_HOST, MONGO_PORT)\r\n}\r\n\r\n@Bean\r\npublic MongoOperations mongoOperations(){\r\n&nbsp;&nbsp; &nbsp;return new MongoTemplate(mongoDBFactory());\r\n}\r\n\r\n-Define your document\r\n@Document(collections=&quot;simpleDocument&quot;)&nbsp;\r\npublic class SimpleDocument{\r\n&nbsp;&nbsp; &nbsp;@Id\r\n&nbsp;&nbsp; &nbsp;String id;\r\n&nbsp;&nbsp; &nbsp;String name;\r\n}\r\n\r\nSimpleDocument s1 = new SimpleDocument(&quot;1&quot;,&quot;Siva&quot;);\r\nmongoOperations.save(s1);<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8211; Its document-oriented NoSQL database by 10gen. &#8211; It is scalable, open source, high-performance document oriented DB. &#8211; New nodes can be added to NoSQL databases. Which means we can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[20],"tags":[],"class_list":["post-311","post","type-post","status-publish","format-standard","hentry","category-nosql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p70lnf-51","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=311"}],"version-history":[{"count":2,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/311\/revisions\/314"}],"wp:attachment":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}