MongoDB {name: “mongo”, type: “db”}

MongoDB {name: "mongo", type: "db"}

<strong opinions>

Whoah! That was my 1st and 2nd, and 3rd.. and goes on… and on.. impression of this amazing database. Having experience with SQL Server and MySQL (both relational databases) for a few good years I decided to take this NoSQL database for a real world run. I also think the massive signs for MongoDB conference in San Francisco on the 101 had subliminally stamped a mark on my neurons 😉 I also read some interesting articles here and here comparing MongoDB to other NoSQL solutions.. After all that I was convinced that MongoDB would be the NoSQL database I would invest some serious time into.

As expressed above, I was impressed by MongoDB. I hooked it into a Zend MVC (Model–view–controller) application accessing Shanty-Mongo ORM through custom Model classes which I wrote from ground up. Everything just fit in so snugly.. and when I threw data against MongoDB it created collections (SQL world you’d call this tables) on the fly. Yes on the fly! That was super cool – loosely coupled interfacing – > create a Class Model and let the DB handle the rest. Super cool. Plus, this baby just flies! Everything from how fast it retrieves, stores, updates (even partial updates) and searches your data to how it stores it as Binary JSON both on the file structure and in memory (during open connection) on the server to speed things up. Everything about this database is impressive.

</strong opinions>

Ok enough of my ramblings. I think you get the picture. I am impressed.

If you are impressed and want to give MongoDB a try, read on. Next let’s dig in and explore stuff that is important (and what I learnt) about MongoDB, how to set it up and common commands to keep handy when working in the terminal.

Hello MongoDB

“MongoDB (from “humongous”) is a scalable, high-performance, open source, document-oriented database. MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS systems (which provide rich queries and deep functionality).” ~ from MongoDB

Then (RDBMS) and now

  • Tables as you know in SQL are called “collections” in MongoDB.
  • Relational DB has records (record sets), MongoDB calls them “documents”.
  • MongoDB stores all data in JSON objects and serialized to BSON (Binary JSON) for storage. CouchDB (you may also know of) stores in just JSON.
  • In MongoDB, “ObjectId” in a collection is similar to auto-incrementing ID in a Relational database table.
  • Here’s a nice mapping chart between SQL and Mongo: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

More FYI notes:

  • You never create a database or collection. MongoDB does not require that you do so. As soon as you insert something, MongoDB creates the underlying collection and database.
  • If you query a collection that does not exist, MongoDB treats it as an empty collection.
  • Switching to a database with the use command won’t immediately create the database – the database is created lazily the first time data is inserted. This means that if you use a database for the first time it won’t show up in the list provided by `show dbs` until data is inserted.
  • Mongo uses memory mapped files to access data, which results in large numbers being displayed in tools like top for the mongod process. Think performance! You can get a feel for the “inherent” memory footprint of Mongo by starting it fresh, with no connections, with an empty /data/db directory and looking at the resident bytes.

Installing MongoDB on a Debian OS (Ubuntu)

If you’re using Ubuntu Server (I used 10.10), you can also install MongoDB using aptitude. Default Ubuntu sources do not contain MongoDB so you need to add distro location to your /etc/apt/sources.list file. That is easily done. 1st open sources.list in terminal editor (nano) like this:

sudo nano /etc/apt/sources.list

and drop & save this line to the end of the file:

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Exit nano and add the following 10gen GPG key, or apt will disable the repository (apt uses encryption keys to verify the repository is trusted and disables untrusted ones).

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Now your ready to install the package with aptitude by executing the following commands:

shell> sudo apt-get update
shell> sudo apt-get install mongodb-10gen

Finally, fork mongo as a Daemon (to auto run on boot).

shell> sudo mongod --fork --logpath /var/log/mongodb.log --logappend

You can now use the command-line client to access the MongoDB database server, as below:

shell> mongo

You may want to hook up MongoDB to be accessible from your PHP application by editing your php.ini and allowing mongo to run as an extension. 1st find & edit your php.ini location like this:

sudo find / -name php.ini
sudo nano /php.ini

Then add & save mongo as an extension inside php.ini under “Dynamic Extensions”:

extension=mongo.so

Save & restart Apache:

sudo /etc/init.d/apache2 restart

Common MongoDB commands

Here’s a short list of the most common commands you will end up using when interfacing with the database. If you want to use a GUI to access MongoDB, I found MongoHub the best GUI administration tool for Mac. There is also a very comprehensive MongoDB documentation located here.

Purpose Shell Command
Login to interface mongo
Show all dbs on record show dbs
Switch to my database use mydb
Show all collections on record show collections
List db version db.version()
Insert data into a new collection db.items.insert({ name:’eggs’, quantity: 10, price: 1.50 })
Display a whole list of documents in a collection db.items.find({})
Display a select list of documents in a collection db.items.find({guid:xyz})
Remove a whole list of documents in a collection db.guid.remove({})
or where n == 1
db.things.remove({n:1});
Drop the collection db.<>.drop()

Cons: when you delete a document you cannot return it’s ObjectId. Would be nice to have this feature. MongoDB folks?

MongoDB start ritual (habit forming):

  1. mongo
  2. show dbs
  3. use mydbname
  4. show collections

This will become a habit so don’t resist.

Don’t forget to read the follow up to this post located here: http://www.theroadtosiliconvalley.com/technology/mongodb-update/
Plenty of new knowledge on tools, crash recovery & best practices.

Give MongoDB a spin

If you are in doubt, give MongoDB a spin and make up your own mind ~ http://www.mongodb.org/
Don’t forget to let me know how your experience goes.. and if you have questions on getting this setup please contact me and I will be more than happy to help you out!

~ Ernest

Recommended links

Making the switch from Windows to Kubuntu

I finally made the switch from Windows Vista to Linux Free Operating System. I moved to the Kubuntu version of Ubuntu 10.10 (a Linux flavour) as my development box and haven’t looked back. Well I lied, since I did look back a bit at the beginning lol. It has been a an interesting challenge mentally adjusting to new way of doing things, new tools (applications) and driver support. In the end it was definitely worth it.

And why Kubuntu? since it’s basically Ubuntu a Debian-derived Linux distribution with KDE (a prettier desktop) on-top. Ubuntu brings your slower machines to life. While Windows keeps on slowing them down. Ubuntu is a secure, intuitive operating system that powers desktops, servers, netbooks and laptops. Ubuntu is, and always will be, absolutely free. More about it here.

Why I switched

Today all my development is open source. This means I run what I create on a LAMP stack – L stands for Linux Server. Doing development on a Windows box and pushing to a LAMP stack is like clawing your way through quick sand instead of using a ninja sword to slice through your tasks.

One day, I asked myself. Wouldn’t it be kick ass if my dev box would be close to identical to my production boxes. Knowing that whatever I do on my dev box will work in production with high certainty. Yes yes, Ubuntu popped into my mind. Which later after speaking with a fellow Linux hacker changed to Kubuntu.

As you may already know, Kubuntu is highly configurable. You even have access to the source code if you wish to venture that deep. It also has a great X window called KDE. Check out these top the winners from a 5-day competition on Facebook where fans were invited to submit a screenshot of their pimped Ubuntu desktop. No excuses about Ubuntu’s poor UI.

My customized Ubuntu desktop
My customized Kubuntu desktop

Linux apps to replace your Windows apps

Here is a comprehensive list of apps to replace your Windows versions.

Note: Most applications & games on Linux are open source. This mostly means free. Thus, the ones I listed below as alternatives in the Linux world are all free and can be downloaded from your package manager. I use Synaptic Package Manager (SPM). All the software here is verified and malicious free – it’s safe to get all your apps from here. To install SPM, in your terminal window type this in and your done. Simple eh.

sudo apt-get update
sudo apt-get install synaptic

Securitythis one just kills windows. Ubuntu comes with a firewall built in and windows viruses – what are they on Ubuntu – non existent. All you need is software like Gufw to help you “manage” your firewall else you can do it via the terminal / konsole window.
In your terminal window type this in and your done. This cannot get any harder 😉

sudo apt-get install gufw

And if you want hard-core detail on securing Ubuntu, read this post covers the process of securing and hardening the default Debian GNU/Linux distribution installation.

Applications… the following let’s use “Synaptic Package Manager”.

Purpose Windows Linux
Development
Code editor Notepad++ gedit
SFTP, FTP and SCP client WinSCP FileZilla
Telnet/SSH Putty OS Konsole /
terminal window
Code compare Beyond Compare Kompare
MySQL manager and admin tool SQLyog MySQL Workbench
Virtualization VMWare VirtualBox
Multimedia
Video player Windows Media Player VLC
Video editor Sony Vegas Kdenlive
Organize, share & edit your photos Picasa Picasa /
Gwenview
Photo editor Photoshop GIMP
Audio player Windows Media Player Amarok
CD/DVD burner Nero K3b
Other
Office (word, excel, powerpoint etc) Windows Office OpenOffice /
Google Docs
File browser Windows Explorer Dolphin
Internet browsers Chrome Chromium
Antivirus & Firewall Take a pick lol Gufw to manage your Firewall
Silverlight MS Silverlight Moonlight

Additional stuff you can install to make your Kubuntu experience pleasing:

Don’t forget to use your Synaptic Package Manager to look for these apps first. Only when you cannot find them there click on the title of each app below to take you to the website hosting the app and instructions.

  • Docky – shortcut bar that sits at the bottom, top, and/or sides of your screen. You can make it look and behave like mac’s bar.
  • KSnapshot – simple & powerful easy to use screen capture program.
  • Ubuntu Tweak – tweak Ubuntu’s desktop and system options that the default desktop environment doesn’t provide.
  • Beagle – advanced desktop search.
  • FreeMind – premier free mind mapping software written in Java.
  • Etherape – graphical network monitor.
  • Other code editors:
    • JetBrain. Their professional developer tools are kickass! I have trialled their PHPStorm & ReSharper with positive results. They also have editors for Ruby & Python (shakes of excitement). It’s not free but they do have trial versions available for download.
    • Eclipse. Open source IDE editors written in Java.
  • Dropbox – Online backup, file sync, and sharing made easy. Get it here: http://db.tt/QDC0nvU
  • ubuntu-restricted-extras – Essential software which is not already included due to legal or copyright reasons. Gives support for MP3 playback and decoding, Java runtime environment, Microsoft fonts, Flash plugin, DVD playback, and LAME (to create compressed audio files).
  • Adobe Flash & Adobe Air so you can run web applications like TweetDeck.

Missing Windows app/s?

If you still miss or cannot find your favorite Windows applications on Kubuntu, you install Wine to run them on Kubuntu. Wine is a program that offers a compatibility layer allowing Linux users to run some Windows-native applications inside of Linux. You can get Wine from Synaptic Package Manager / package manager or by following the instructions here.

Stuff I still need my Windows box for

  • Photo editing – Photoshop and Lightroom and
  • Video editing – Sony Vegas (goes with my Sony HD cam). The Linux alternative Kdenlive just dosent cut it.

With time I’m sure a super duper speced up Mac (with Dual boot for Kubuntu) will replace both my laptops. Now I need to sell myself why I should move to a Mac and pay double the price for hardware.

PS. If you have suggestions or additions to this post please comment below or contact me.

Happy hacking!

~ Ernest