A CodeIgniter active record library for MongoDB

This evening I started work on an active record inspired library for interacting with MongoDB via a CodeIgniter library.

So far I’ve got searching and inserting working. Updating and deleting are going to take more time because they’re a bit trickier.

What this means is you can easily work with a database collection like so:

$this->mongo_db->where_gte('age', 18)->where(array('country' => 'UK', 'can_drink' => TRUE))->get('people');

Or:

$this->mongo_db->get_where('posts', array('title' => 'Hello, World!');

Or just:

$this->mongo_db->get('sales');

I’ve created a repository on Bitbucket located at http://bitbucket.org/alexbilbie/codeigniter-mongo-library. If you have a look at the README file there is an overview of what is there so far and also what needs doing (and some maybes!).

Hope people find it useful.

12 thoughts on “A CodeIgniter active record library for MongoDB

  1. Hi

    You can add an example to use in Geospatial Indexing
    By using the functions:

    Where_near
    and
    Set_index

    Thanks for your help

    Moshe

  2. Hello ,

    thanks for the post and the library, how can i use update function to update collections with nested arrays

    for example

    db.blog.posts.update({“title” : “A blog post”}, {$push : {“comments” :
    {“name” : “joe”, “email” : “joe@example.com”, “content” : “nice post.”}}})

    how can i do it with the library ?

    if i want to push more comments to the same post

    would you please give me example like

    $this->mongo_db->update(‘posts’ ,$data= array(‘name’ => ‘test’ ), array(‘$push’ =>$comments));

    Regards

  3. Hello I’ll give another full example and i don’t know where is the problem

    I have this collection

    Array (4)
    (
    | [‘conv_date’] = String(21) “2010-11-24 09: 37: 14”
    | [‘conv_id’] = Integer(1) 1
    | [‘conv_title’] = String(11) “the title 2”
    | [‘messages’] => Array (2)
    | (
    | | [‘0’] => Array (7)
    | | (
    | | | [‘attch_id’] = Integer(1) 0
    | | | [‘msg_attch_type’] = NULL(0) NULL
    | | | [‘msg_body’] = String(17) “this is msg2 body”
    | | | [‘msg_date’] = String(21) “2010-11-24 09: 37: 14”
    | | | [‘msg_id’] = Integer(1) 1
    | | | [‘msg_title’] = String(11) “the title 2”
    | | | [‘sender_id’] = Integer(2) 19
    | | )
    | | [‘1’] => Array (2)
    | | (
    | | | [‘msg_id’] = Float(1) 3
    | | | [‘msg_title’] = String(15) “this is title 3”
    | | )
    | )
    )

    and i can insert new message to messages array by using the terminal with this command

    db.msgs_conv.update({‘conv_id’:1},{$push : {‘messages’:{‘msg_id’:3,’msg_title’:’this is title 3′}}})

    and every thing working fine

    i don’t know how to do the same with your library

    $this->mongo_db->update(‘msgs_conv’,array(‘conv_id’ => 1), array(‘$push’ =>’messages’,$message));

    i don’t know why it’s not working

  4. […] В заключение хочу оставить еще одну ссылку, откуда я и «одолжил» весь процесс установки MongoDB – в довесок там имеются примеры по работе с MongoDB в CodeIngiter (статья на английском): http://alexbilbie.blogs.lincoln.ac.uk/2010/04/17/a-codeigniter-active-record-library-for-mongodb/ […]

  5. A CodeIgniter active record library for MongoDB…

    This evening I started work on an active record inspired library for interacting with MongoDB via a CodeIgniter library. So far I’ve got searching and inserting working. Updating and deleting are going to take more time because they’re a bit trickier….

Leave a Reply

Your email address will not be published.