Zend Framework and mongoDB – Getting Started Quickly!


This is a mini-post showing you how to get up and running with mongoDB and Zend Framework using a series of third-party libraries, including Shanty-Mongo library. To be honest, it’s rather trivial, but sometimes it can be frustrating trying to find a simple, concise, reference on how to do it, specific to just your needs.

What’s Mongo? If you need a great introduction to Mongo? Grab a copy of the Little MongoDB Book by Karl Seguin.
Already know MongoDB? Check out our follow up post on using mongoDB and Zend Framework to create a simple blog.

Installing mongoDB

This is really dependent on the platform that you’re on. The platform that I prefer for development is definitely Ubuntu. The latest stable release at the time of writing is 10.10. However there is a slight bug in the default mongoDB package for Ubuntu. You can find more detailed information here. But to summarise the process for getting mongoDB up and running quickly on Ubuntu, follow these instructions:

  1. add deb http://downloads.mongodb.org/distros/ubuntu 10.10 10gen to /etc/apt/sources.list
  2. sudo apt-get update (I’m assuming that no errors occur)
  3. sudo apt-get install mongo-stable

Let’s make it even simpler. Simply run the following after point one above:

  1. sudo apt-get update && sudo apt-get install mongo-stable

Alternatively, you can go the more traditional route and download the source and the run the ./compile, make, make install and make test route. Personally, using a package manager such as APT is just so easy as the real focus of what we’re after here is to develop, not install packages. But there are and always will be, benefits to compiling it yourself.

The case of the “Mysteriously lingering PID file”

Sometimes after installing, re-installing, or attempting to restart mongoDB on Ubuntu there is a lingering pid file that you need to remove and then restart mongoDB. After that you will have a running mongoDB daemon with no security set – so any user can no manage records through it. To test this, run the following command:

  • sudo netstat -tlnp | grep mongo

If you see a listing for mongo, then you’re ready to start working with it.

Shanty-Mongo Library

In the application configuration ini file (application/config/application.ini), add the following in the production section:

  • autoloaderNamespaces[] = 'Shanty_Mongo'

This has now made the Shanty-Mongo library available to your application.

Adding Security

Now in a development environment, adding security is not always necessary, but it can be good practice to enforce it right from the get-go. If you would like to read more about how to set it up, head on over to the Security and Authentication section of the mongoDB website.

Available Third-Party Libraries

Now Shanty-Mongo isn’t the only add-on library available for Zend Framework to work with mongoDB. Have a look at the list below for a range of alternatives.

You can find a list of PHP-based libraries, for use with the Zend Framework, as part of other projects and standalone at http://www.mongodb.org/display/DOCS/PHP+Libraries,+Frameworks,+and+Tools.

Finishing Up

After that, you’re done and ready to go. You should follow-up this post by reading Coen’s excellent README either in the library, or via the project page on https://github.com/coen-hyde/Shanty-Mongo. It’s written in a very clear and logical manner and you’ll be up and running in no time at all. If you do have any troubles, please don’t hesitate to contact him, or contact us here at Malt Blue.

Coming Soon

Have you heard about MapReduce? Want to use it in your Zend Framework project? Stay tuned for our next post when we extend on this one to start showing you just that!

Follow-Up Reading

If you liked what you read and would like to see more, please retweet it, or give it a like on Facebook or even give it some Digg love. And we always value you feedback and comments.

Matt's Pic About Matthew Setter

Matthew Setter, a passionate Australian, is the founder and chief-editor of Malt Blue. Follow him on Twitter at @settermjd and find out more about him on the team page.

Intermediate MongoDB PHP Tutorial Zend Framework