In the world of NoSQL-Hands on Mongodb-1

I’m hearing a lot of “NoSQL” 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, scalable, high-performance, schema-free, document-oriented database written in the C++ programming language.

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).

This post will surely get you started with the MongoDB-NoSQL.

Installing MongoDB:

  • Download Mongodb from www.mongodb.org
  • Extract binaries
  • Create data directory – default for windows C:\data\db
  • Install service: Full\path\to\mongod.exe –install

If you want to change default data directory location:
Full\path\to\mongod.exe –dbpath=”\”full\path\to\”” –install

Troubleshooting Mongodb Installtion Error:
I faced while installing, if you get error like:
dbexit:
shutdown: going to close listening sockets.
shutdown: going to flush oplog…
shutdown: going to close sockets…
shutdown: waiting for fs preallocator…
shutdown: closing all files…
closeAllFiles() finished
dbexit: really exiting now

Go to: HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/
Check for MongoDB
Verify the value of ImagePath is proper

This seems to be caused by not giving full-path while installing.

Add Environment variable Path, add path/upto/mongo-bin-dir;

Okay, now we’re all set to checkout the MongoDB-NoSQL world.

MongoDB GUI Administration Tool:

There is a GUI Admin tool available phpmoadmin to simplify the MongoDB Operations.
Signle php script: phpmoadmin
Download PHP extension from php.net/manual/en/mongo.installation.php
Extract in ext directory of WAMP & Restart.

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&displaylang=en]

MySQL to MongoDB Database Migration:

Create CSV from MySQL:
mysql> select * into outfile “c:/TABLENAME.csv” FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’ from TABLENAME;
Query OK, 293911 rows affected (1 min 8.17 sec)


Import CSV to mongodb using mongoimport:
C:\>mongoimport -d DATABASENAME -c TABLENAME –file “c:\TABLENAME.csv” –type csv -f FIELD1,FIELD2…

Using MongoDB on Windows:

On Command Prompt:
C:\MongoDB\mongodb-win32-i386-1.4.0\bin>mongo.exe
MongoDB shell version: 1.4.0
url: test
connecting to: test
type “exit” to exit
type “help” for help

By default it’s connecting to test database.
Default mongodb port is 9999.

Basic Commands

  • show dbs : show database names
  • show collections : show collections in current database
  • show users : show users in current database
  • show profile : show most recent system.profile entries with time >= 1ms
  • use : set curent database to
  • db.help() : help on DB methods
  • db.foo.help() : help on collection methods
  • db.foo.find() : list objects in collection foo
  • db.foo.find( { a : 1 } ) : list objects in foo where a == 1 it result of the last line evaluated; use to further iterate

Simple Queries with MongoDB:

Set Current Database:
>use DATABASENAME


See Present Database:
> db
DATABASENAME


View collections (tables) under present db:
> db.getCollectionNames()
[ “TABLENAME”, “system.indexes” ]


SQL>Select count(*) from TABLENAME:
NoSQL> db.TABLENAME.count()
238735


SQL>select * from TABLENAME where field1 regexp ‘enrique’:
NoSQL>db.TABLENAME.find( { field1 : /enriqu/i} );


For simple find:
NoSQL>db.TABLENAME.find( { field1 : “value” } );


SQL>Select field1 from table where field2=”%search%”:
NoSQL>db.TABLENAME.find( { field2 : /search/i} , {field1 : 1 } );


SQL>Select All but one (field1) column:
NoSQL>db.TABLENAME.find( { field2 : /search/i} , {field1 : 0 } );


SQL>Select with sort order ascending and descending (1,-1):
NoSQL>db.TABLENAME.find( { ar_name : /enrique/i }, {field1 : 1 } ).sort({ field1 : -1})

MongoDB now seems easy to follow, but I’m still having hard time in taking on the Map/Reduce.
More in the next post; ofcourse documentation home is a good place, else you may export the pdf / html / xml documentation here.

2 comments
  1. Phpmoadmin not working for me

    I tried a lot but Phpmoadmin was not working for me. My problem was as follows:

    I have wamp – c:\wamp
    I’ve installed mongodb – c:\mongodb\…
    Installed mongodb as a windows service
    Set environment variable Path
    Installed Microsoft Visual C++ 2008 SP1 Redistributable Package (x86)
    so the php_mongo extension is now visible-enable in php extensions.
    Updated php.ini and restarted wamp
    Things looks fine here.
    Further I copied phpmoadmin.php to wamp\www\MongoAdmin\phpmoadmin.php
    I’m still receiving error “PHP cannot access MongoDB, you need to
    install the Mongo extension for PHP. Instructions and driver download: vork.us/go/tv27”

    The error in log:

    PHP Warning: PHP Startup: mongo: Unable to initialize module
    Module compiled with module API=20090626, debug=0, thread-safety=1
    PHP compiled with module API=20060613, debug=0, thread-safety=1
    These options need to match
    in Unknown on line 0

    PHP Installed Extensions:

    Array ( [0] => bcmath [1] => calendar [2] => com_dotnet [3] => ctype
    [4] => session [5] => filter [6] => ftp [7] => hash [8] => iconv [9]
    => json [10] => odbc [11] => pcre [12] => Reflection [13] => date [14]
    => libxml [15] => standard [16] => tokenizer [17] => zlib [18] =>
    SimpleXML [19] => dom [20] => SPL [21] => wddx [22] => xml [23] =>
    xmlreader [24] => xmlwriter [25] => apache2handler [26] => gd [27] =>
    mbstring [28] => mysql [29] => mysqli [30] => PDO [31] => pdo_mysql
    [32] => pdo_sqlite )

    The solution to PHPMoAdmin Not working
    Kristina Chodorow spot the correct problem:

    Module compiled with module API=20090626, debug=0, thread-safety=1
    means you’re using a module compiled with the 5.3 API.
    The only 5.2 PHP driver binaries are built with VC6 and it looks like you’re
    running VC8 (you can check in phpinfo(), search for “VC”). It would be
    fairly easy to setup an automatic build for 5.2+VC8, but I can’t until
    Monday.

    The compiled version for 5.2 PHP driver built in VC6 is available here:
    http://downloads.mongodb.org/mongo-latest-php5.2vc6ts.zip

    See:- groups.google.com/group/mongodb-user/browse_thread/thread/8e0e65dd62c60422/68d6a3b717724dfb#68d6a3b717724dfb

Leave a Reply

Your email address will not be published. Required fields are marked *