ActiveMongo

After awhile without blogging, I was quite busy with work and some personal projects. Well, I created my first PECL Package (and therefore I have my @php.net mail) that I built in order to accomplish my personal goal that is already working on my sandbox (I’ll cover this later in this blog).

This time I will talk about MongoDB, and my simple yet efficient ActiveRecord class that I wrote in less than a week, in order to make even easier use MongoDB from PHP.

If you already have any experience with MongoDB, you might be wondering why did you create it? MongoDB is already very simple. That’s right, but I want to make even easier and amicable, so I focused in these things:

Keep it simple stupid.

Easy iteration.

Optimized Updates.

MongoDB is already good for queries, don’t wrap it

Of course, you can do all this with just MongoDB, but it’s a bit tricky, especially the Optimized updates (just updating what had changed).

Until here, there is nothing new, except that for Updates, instead of put the whole object back, ActiveMongo will perform a diff between the current result and the object’s property, and it will generate a special document using $unset, $set that is going to be sent to Mongo. Again, this operation is very simple, but it might be hard to detect, look at the next example:

That is quite hard to generate by hand for every table collection, and put the entire document back is a waster of resource (network, IO and so forth).

Another important feature (at least for me , are the data validation. I implemented it in a simple way (at least I think). Suppose that in the User collection, we want to store the password encoded with SHA1, and the username can’t be changed, this can be done as follow:

Nice, isn’t it?. Of course, we can’t check if a current field exists or not, we can only validate that if exists. If you need to ensure that every document has some properties you can use the pre_save hook that receive as first parameter the operation (‘create’ or ‘update’) and document that will be saved in the second parameter.

If the folk request (and if it is useful) this checking could be automated somehow (e.j: implementing a method checkFields() that return an array of required fields), meanwhile I’ll find this way pretty useful and amicable, also this hook could be used to check if the current user has permissions to perform an insert or creation (useful for CRM, currently ActiveMongo support only three possible hooks, pre_save, on_save (after the save()) and on_iterate (when it moves to the next record).

The most important part is how you query your database, for our luck, MongoDB has a very flexible query method, think on it as a compiled SQL, with no join ), because MongoDB is already kickass in queries, there is no need to abstract it.

Currently ActivoMongo is still under development, but as far as I have tested seems pretty stable, most of development are to add new functionality, for instance I’m looking for an easy way to add references to other document, or set of documents, keeping in mind efficiency, talking as less as possible to the database.

This looks great! I’m interested in learning to use mongoDB in my applications, and I’m familiar with Active Record through CodeIgniter. I would love to be able to use this class in my CI applications. Do you have any plans to post this into Google Code? I would like to continue following your development of this class.

Hey, great work. I discovered mongoDB earlier today, had a quick play with it, and then through some browsing discovered this page. I think that accessing a collection using an object like approach rather than array syntax could be really handy.

The git repository README says, “Don’t use it now.” I’m going to put my project on hold for a while because I want to use it. Will you be able to notifiy us when it is hosted on PHP Classes? I believe that they are set up to notify those who have downloaded a class of any updates to it. Thanks.

I’m finish the first release, I introduced huge changes which attempts to make it even easier to use. I’m currently finishing the testing suites (very important for the project consistency). I believe it would be in PHPClasses in few weeks..