Intekhab Rizvi
All projects

CIMongo: MongoDB library for CodeIgniter (legacy)

A MongoDB library for CodeIgniter. This is the original release; the current version lives on GitHub.

Language
PHP
Framework
CodeIgniter
Database
MongoDB
Status
Legacy release

Overview

CIMongo is a MongoDB library for the CodeIgniter framework. It gives controllers and models a CodeIgniter-style way to talk to MongoDB: build a query by chaining methods such as where(), order_by() and limit(), then run it with get().

This page documents the original release, which stays online for projects that still depend on it. New projects should use the current version on GitHub.

Installation

  1. Download the repository and unzip it. It contains two folders: config and libraries.
  2. Copy both folders into your CodeIgniter application folder.
  3. Check that mongo_db.php now exists in both application/config and application/libraries.

Configuration

Put your MongoDB login details in application/config/mongo_db.php, then autoload the library in application/config/autoload.php:

$autoload['libraries'] = array('mongo_db');

By default the library connects with the default group. In any controller or model, call its methods as $this->mongo_db->.

Quick examples

A few typical calls, to show the style of the API:

// insert a document
$this->mongo_db->insert('users', array('name' => 'Asha', 'age' => 31));

// find documents
$users = $this->mongo_db
    ->where(array('age' => 31))
    ->order_by(array('name' => 'asc'))
    ->limit(10)
    ->get('users');

// update one field
$this->mongo_db->where(array('name' => 'Asha'))->set('age', 32)->update('users');

// delete
$this->mongo_db->where(array('name' => 'Asha'))->delete('users');

These follow the conventions of CodeIgniter MongoDB libraries. Check the method list below and the repository for the exact arguments each method accepts.

Multiple database connections

To talk to more than one MongoDB server or database, add another group to mongo_db.php and give it its own settings: hostname, port, username, password, database, the debug flag, write concerns, journal and read preference. The group can have any name.

Then load the library with the activate parameter set to that group name, plus an alias. In a controller or model constructor:

$this->load->library('mongo_db', array('activate' => 'second'), 'mongo_db2');

$rows = $this->mongo_db2->get('users');

The second connection is now available as $this->mongo_db2->, and $this->mongo_db-> keeps using the default group.

Method reference

Every method available in this release, grouped by what it does.

Insert

insertbatch_insert

Find

selectwhereget_wherewhere_inwhere_in_allwhere_not_inwhere_orwhere_gtwhere_gtewhere_ltwhere_ltewhere_betweenwhere_between_newhere_nelikeorder_bylimitoffsetcountdistinctfind_one

Update

setunset_fieldaddtosetpushpoppullrename_fieldincmulmaxminupdateupdate_all

Delete

deletedelete_all

Aggregation

aggregate

Profiling

output_benchmark

Indexes

add_indexremove_indexlist_indexes

Database

switch_dbdrop_dbdrop_collection

Extras

date