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.