{"id":749,"date":"2010-04-05T11:14:00","date_gmt":"2010-04-05T05:44:00","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=749"},"modified":"2013-01-25T04:28:36","modified_gmt":"2013-01-25T04:28:36","slug":"in-the-world-of-nosql-hands-on-mongodb-1","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/in-the-world-of-nosql-hands-on-mongodb-1","title":{"rendered":"In the world of NoSQL-Hands on Mongodb-1"},"content":{"rendered":"<p>I&#8217;m hearing a lot of &#8220;NoSQL&#8221; these days. To really understand how (and) does it works, I decided to give a try on MongoDB.<\/p>\n<p>MongoDB (hu*mongo*us) is an open source, scalable, high-performance, schema-free, document-oriented database written in the C++ programming language.<\/p>\n<p>MongoDB is not a Relational Database Management System. The database manages collections of JSON (JavaScript Object Notation)-like documents which are stored in a binary format referred to as BSON (Binary JSON).<\/p>\n<p>This post will surely get you started with the MongoDB-NoSQL.<\/p>\n<h1>Installing MongoDB:<\/h1>\n<ul>\n<li>Download Mongodb from www.mongodb.org<\/li>\n<li>Extract binaries<\/li>\n<li>Create data directory &#8211; default for windows C:\\data\\db<\/li>\n<li>Install service:\u00a0Full\\path\\to\\mongod.exe &#8211;install<\/li>\n<\/ul>\n<p>If you want to change default data directory location:<br \/>\nFull\\path\\to\\mongod.exe &#8211;dbpath=&#8221;\\&#8221;full\\path\\to\\&#8221;&#8221; &#8211;install<\/p>\n<p><strong>Troubleshooting Mongodb Installtion Error:<br \/>\n<\/strong> I faced while installing, if you get error like:<br \/>\n<em> dbexit:<br \/>\nshutdown: going to close listening sockets.<br \/>\nshutdown: going to flush oplog&#8230;<br \/>\nshutdown: going to close sockets&#8230;<br \/>\nshutdown: waiting for fs preallocator&#8230;<br \/>\nshutdown: closing all files&#8230;<br \/>\ncloseAllFiles() finished<br \/>\ndbexit: really exiting now<\/em><\/p>\n<p>Go to:\u00a0<em>HKEY_LOCAL_MACHINE\/SYSTEM\/CurrentControlSet\/Services\/<\/em><br \/>\nCheck for <em>MongoDB<\/em><br \/>\nVerify the value of <em>ImagePath <\/em>is proper<\/p>\n<p>This seems to be caused by not giving full-path while installing.<\/p>\n<p>Add <em>Environment variable Path<\/em>, add path\/upto\/mongo-bin-dir;<\/p>\n<p>Okay, now we&#8217;re all set to checkout the MongoDB-NoSQL world.<\/p>\n<h1>MongoDB GUI Administration Tool:<\/h1>\n<p>There is a GUI Admin tool available phpmoadmin to simplify the MongoDB Operations.<br \/>\nSignle php script: phpmoadmin<br \/>\nDownload PHP extension from php.net\/manual\/en\/mongo.installation.php<br \/>\nExtract in ext directory of WAMP &amp; Restart.<\/p>\n<p>In case of error you may install Microsoft Visual C++ 2008 SP1 Redistributable Package (x86) [www.microsoft.com\/downloads\/details.aspx?familyid=A5C84275-3B97-4AB7-A40D-3802B2AF5FC2&amp;displaylang=en]<\/p>\n<h1>MySQL to MongoDB Database Migration:<\/h1>\n<p>Create CSV from MySQL:<br \/>\n<em> mysql&gt; select * into outfile &#8220;c:\/TABLENAME.csv&#8221; FIELDS TERMINATED BY &#8216;,&#8217; OPTIONALLY ENCLOSED BY &#8216;&#8221;&#8216; LINES TERMINATED BY &#8216;\\n&#8217; from TABLENAME;<br \/>\nQuery OK, 293911 rows affected (1 min 8.17 sec)<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>Import CSV to mongodb using mongoimport:<br \/>\n<em> C:\\&gt;mongoimport -d DATABASENAME -c TABLENAME &#8211;file &#8220;c:\\TABLENAME.csv&#8221;  &#8211;type csv -f FIELD1,FIELD2&#8230;<\/em><\/p>\n<h1>Using MongoDB on Windows:<\/h1>\n<p>On Command Prompt:<br \/>\nC:\\MongoDB\\mongodb-win32-i386-1.4.0\\bin&gt;mongo.exe<br \/>\nMongoDB shell version: 1.4.0<br \/>\nurl: test<br \/>\nconnecting to: test<br \/>\ntype &#8220;exit&#8221; to exit<br \/>\ntype &#8220;help&#8221; for help<\/p>\n<p>By default it&#8217;s connecting to test database.<br \/>\nDefault mongodb port is 9999.<\/p>\n<p><strong>Basic Commands<br \/>\n<\/strong><\/p>\n<div id=\"_mcePaste\">\n<ul>\n<li>show dbs : show database names<\/li>\n<li>show collections : show collections in current database<\/li>\n<li>show users : show users in current database<\/li>\n<li>show profile : show most recent system.profile entries with time &gt;= 1ms<\/li>\n<li>use : set curent database to<\/li>\n<li>db.help() : help on DB methods<\/li>\n<li>db.foo.help() : help on collection methods<\/li>\n<li>db.foo.find() : list objects in collection foo<\/li>\n<li>db.foo.find( { a : 1 } ) : list objects in foo where a == 1 it result of the last line evaluated; use to further iterate<\/li>\n<\/ul>\n<\/div>\n<h1>Simple Queries with MongoDB:<\/h1>\n<p><em>Set Current Database:<br \/>\n&gt;use DATABASENAME<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p><em>See Present Database:<br \/>\n&gt; db<br \/>\nDATABASENAME<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>View collections (tables) under present db:<br \/>\n<em> &gt; db.getCollectionNames()<br \/>\n[ &#8220;TABLENAME&#8221;, &#8220;system.indexes&#8221; ]<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>SQL&gt;Select count(*) from TABLENAME:<br \/>\n<em> NoSQL&gt; db.TABLENAME.count()<br \/>\n238735<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>SQL&gt;select * from TABLENAME where field1 regexp &#8216;enrique&#8217;:<br \/>\n<em> NoSQL&gt;db.TABLENAME.find( { field1 : \/enriqu\/i} );<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>For simple find:<br \/>\n<em> NoSQL&gt;db.TABLENAME.find( { field1 : &#8220;value&#8221; } );<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>SQL&gt;Select field1 from table where field2=&#8221;%search%&#8221;:<br \/>\n<em> NoSQL&gt;db.TABLENAME.find( { field2 : \/search\/i} , {field1 : 1 } );<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>SQL&gt;Select All but one (field1) column:<br \/>\n<em> NoSQL&gt;db.TABLENAME.find( { field2 : \/search\/i} , {field1 : 0 } );<\/em><\/p>\n<p><em><br \/>\n<\/em><\/p>\n<p>SQL&gt;Select with sort order ascending and descending (1,-1):<br \/>\n<em> NoSQL&gt;db.TABLENAME.find( { ar_name : \/enrique\/i }, {field1 : 1 } ).sort({ field1 : -1})<\/em><\/p>\n<p>MongoDB now seems easy to follow, but I&#8217;m still having hard time in taking on the Map\/Reduce.<br \/>\nMore in the next post; ofcourse documentation home is a good place, else you may export the pdf \/ html \/ xml documentation <a href=\"http:\/\/www.mongodb.org\/display\/DOCS\/Downloads\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"I&#8217;m hearing a lot of &#8220;NoSQL&#8221; these days. To really understand how (and) does it works, I decided to give a try on MongoDB. MongoDB (hu*mongo*us) is an open source,&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[33,69,79,426],"class_list":{"0":"post-749","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-technical","7":"tag-database","8":"tag-mongodb","9":"tag-nosql","10":"tag-technical"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=749"}],"version-history":[{"count":1,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/749\/revisions"}],"predecessor-version":[{"id":1873,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/749\/revisions\/1873"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}