MongoDB PHP Tutorial

→ Are you a new visitor? Please visit the page guidance for new visitors ←

MongoDB PHP Tutorial

I’ve been thinking that a tutorial about using MongoDB with PHP would be needed, a few days ago I went and showed you how to install MongoDB and also to get the php extension for MongoDB installed. So in this article I’m going with a MongoDB php tutorial and trying to get a step by step guide with the most important things you would need to work with it. I’ll try to get most of the things down here and hoping it will help someone starting up with MongoDB.

Making a connection to MongoDB from PHP

So lets get straight to action, the first thing you will need to do is to open a connection to our MongoDB server. This is practically the simplest of them all, from all extensions I’ve worked it, there’s nothing simpler then:

Its practically calling the MongoClient() class and from there we can start using the client methods.

Select a database in MongoDB from PHP

To select a database, I’ve seen this has been changed from before, it was using quotes to specify the database you want to connect to using a specific function, but now the methods just needs the database name (got simpler). Say we have our own database name exampleDB created on mongoDB server, using the line below we select the database just like that:

At this point we are using the database exampleDB and we can start working on it.

Select data from the database from php

This is done by first selecting the collection from the database and then issuing the find() method. Keep in mind that collections in MongoDB is similar to tables in normal relation SQL databases. These are usually called objects or documents.

After we have the collection we want selected, we can then issue the find() method on it and build our little object array:

From here its simple like working with arrays and iterating through it:

Inserting data into your database from php

To insert a new document into your database collection you need to know exactly how you structured the collection, in my case I only have a simple name id that I’m using for this example. But advanced collections can have multiple levels under each ID so be careful you have it written down somewhere so that you can always see it. Its really easy to insert data in the collection also, basically you build an array similar to your collection structure, for instance:

This a simple case where you’re adding records to the same ID, however you can choose a different ID and shape directly:

The end result when dumping the MongoCursor array would look similar to:

Noticed how the last object there is a bit different the previous ones?

Updating records on mongoDB in PHP

This can be done similarly how I showed you the insert, only this time we are using the update() method. This will also require a $where option so that we know which document from our collection needs updated. For example:

This should update the name to Matthew where the active index is marked as true.

That would be all for now, see you in our next next tutorial!

Request an article ←