MongoDB in Codeigniter

MongoDB (from “humongous”) is a scalable, high-performance, open source NoSQL database.

MongoDB is non-relational (no joins) so references (“foreign keys”) between documents are generally resolved client-side by additional queries to the server (“linking”).

To start you need to Download and install MongoDB

At this point you should be able to run the console/terminal.

After that you need to install the drivers onto your server

So once you have your server running MongoDB and installed your drivers then the following command should not produce any errors when entering this into your PHP code.

$this->connection = new Mongo('localhost:27017');

If you do not have any errors then you should be good to continue.

My full working example is available at

This example allows you to add/edit/delete new members to your NoSQL database.

I’m not going to describe how to create and manage Controllers/Models/Views as these are CodeIgniter basics.

In your controller you populate your constructor:

This calls basic helpers to manage urls and date formatting.
Also I have called my Model Users here as each function requires access to this data.

Then in the index function:

This calls my Model to retrieve the data, loops through all results and enters it into an array to pass to the view.
One point to note is that I am using $member[“_id”]->__toString()
This is the unique id for each record created by MongoDB so there is no need to create an additional field called ID to our data.

Now to create the Model.
These are some general functions that would be useful for many different requests that you require.

Inside the constructor I connect to Mongo with localhost:27012, create a database called blog (can be any name you wish) and set the table name as users.
This now means that all calls made to $this->posts will now be made on the users table.

As you can see in the function getAll() call we get all data inside the users table with the call $this->posts->find();
After that point you can then limit, order by and other functions if you require.

Then on the View you only need to add

This will list out all the results in the $members array displaying the name of the member.

Have a look through the GitHub repo to see how to add/edit/delete members.
Its all very similar just with different syntax.

Hope this helps you get going on a NoSQL site.

2 comments for “MongoDB in Codeigniter

  1. January 21, 2012 at 5:51 pm

    Looks like you can do quite a bit with just a little code! Seems like a good approach just to give MongoDB a try, like for myself I have only played with CouchDB. Otherwise you may consider using as a framework, that also has support for NoSQL.

  2. Maridansoft
    May 21, 2015 at 3:07 pm

    How would you save a file to mongodb with gridFS in codeigniter???

    I have opened an issue here ….
    Could you please be so kind to show?

Leave a Reply

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