Updates to CodeIgniter MongoDB library

This library can now be found at https://github.com/alexbilbie/codeigniter-mongodb-library and has been updated with numerous bug fixes since this post was written. If you find a new bug please add it to the issue tracker. Thanks!

I’ve spent some time this evening updating my CodeIgniter MongoDB library. You can get the latest release (4.0.1 at time of writing) at https://bitbucket.org/alexbilbie/codeigniter-mongo-library/

hg clone https://bitbucket.org/alexbilbie/codeigniter-mongo-library

Or if you’re one of the cool kids and using Sparks run

php tools/spark install -v4.0.1 mongodb

So what’s new?

You can now pass a mongo id in where and it will automatically be converted to the correct MongoId object type. You can also pass a field and value to the where function instead of an array. Thanks to Phil Sturgeon for this.

$this->mongo_db->where('_id', 'ced141265b96c037a3cab9dee0f3b4fa')->get('post')

I’ve also added a lot of new update functions which should make expressing updates much easier. Now you can write queries like:

$this->mongo_db
->where('_id', 'ced141265b96c037a3cab9dee0f3b4fa')
->set('title', 'My new blog post')
->inc('comment_count', 1)
->push('comments', array('id'=>1, 'name'=>'Alex', 'text'=>'Hello, world!'))
->update('post')

The new functions you can use are:

  • inc – increment the (integer) value of a field
  • dec – decrement the (integer) value of a field
  • set – sets the field to a new value
  • unset – unsets a field
  • push – pushes a new element into an array
  • pop – pops the last element from an array
  • pull – removes all occurrences of value from field
  • rename_field – renames a field (key remains intact)

There are a few missing functions that I couldn’t get to work this evening but I’ll add them shortly:

  • push_all – appends each value (where value is an array) to field
  • pull_all – removes all occurrences of value (where value is an array) in field
  • bit – does a bitwise update of a field
  • add_to_set – adds value to the array only if its not in the array already, if field is an existing array, otherwise sets field to the array value if field is not present

There are a number of other small enhancements, and I’ve updated the licence to the MIT License.

9 thoughts on “Updates to CodeIgniter MongoDB library

  1. Is it possible, that _id only supports the MongoID object? I can change _id to any var type, but then e.g. get_where() doesn’t works anymore. I’m fairly new to MongoDB development, but in O’Reillys “Definitive Guide” i read, it’s possible to define _id to any data type. Currently I’m using my own field id besides _id, but I think, that’s rather messy. 😉

  2. Hey Alex. Is it possible to combine queries with an or_where, for instance:

    $set_five = $this->mongo_db->or_where(array(“$eq”=>array(‘doctor_arrived’=>0), ‘$gte’=>array(‘timestamp’, 0) ) )->get(‘oc_time’);

    Or maybe is there another way to combine complex AND-OR sequences that need tests with, for instance, items greater than some value OR items less than another

    • To do an use where() for AND and or_where() for OR:

      e.g. Select where age is 18 AND sex is male OR female

      $this->mongo_db->where(array(‘age’=>18))->or_where(array(‘sex’=>’male’)->or_where(array(‘sex’=>’female’))->get(‘gamers’)

  3. Also had to comment out most of the get function line 591. Something wrong with this code. You may want to do some unit testing on this module.

    /*
    if (isset($this->wheres[‘_id’]) and ! ($this->wheres[‘_id’] instanceof MongoId))
    {
    if( is_array( $this->wheres[‘_id’][key($this->wheres[‘_id’])]) ){ // looks like case of IN or NIN
    foreach($this->wheres[‘_id’][key($this->wheres[‘_id’])] as $i=>$id)
    {
    if( !($id instanceof MongoId) )
    {
    $this->wheres[‘_id’][key($this->wheres[‘_id’])][$i] = new MongoId($id);
    }
    }
    }else{
    $this->wheres[‘_id’] = new MongoId($this->wheres[‘_id’]);
    }
    }
    */

  4. line 239 @usage : $this->mongo_db->or_where(array( array(‘foo’=>’bar’, ‘bar’=>’foo’ ))->get(‘foobar’);

    this should be

    $this->mongo_db->or_where(array(‘foo’=>’bar’, ‘bar’=>’foo’ ))->get(‘foobar’);

    I have been informed that several other comments are incorrect or incomplete. As per the previous post.

    * To get an ID which is not a Mongo Object ID (id: 9162), this block of code must be commented out.
    * If getting an ID which IS a Mongo Object ID (id: ObjectId(“47cc67093475061e3d95369d”)), the code below must be uncommented.
    /*
    if (isset($this->wheres[‘_id’]) and ! ($this->wheres[‘_id’] instanceof MongoId))
    {
    if( is_array( $this->wheres[‘_id’][key($this->wheres[‘_id’])]) ){ // looks like case of IN or NIN
    foreach($this->wheres[‘_id’][key($this->wheres[‘_id’])] as $i=>$id)
    {
    if( !($id instanceof MongoId) )
    {
    $this->wheres[‘_id’][key($this->wheres[‘_id’])][$i] = new MongoId($id);
    }
    }
    }else{
    $this->wheres[‘_id’] = new MongoId($this->wheres[‘_id’]);
    }
    }
    */

Leave a Reply

Your email address will not be published.