[
  {
    "path": "README.md",
    "content": "[![Build Status](https://travis-ci.org/intekhabrizvi/Codeigniter-mongo-library.svg?branch=unitesting)](https://travis-ci.org/intekhabrizvi/Codeigniter-mongo-library)\n=======\nCIMongo - MongoDB Library for Codeigniter (Legacy)\n=======\nFor PHP7+, Codeigniter3+ and mongodb-1.5-x php extension support, use the latest library available at https://github.com/intekhabrizvi/codeigniter-mongodb-library\n=======\n\n## We tried to make transition as easy as possible by keeping all functions name as is. In most cases new library would be a drop-in replacement for this library, if not please open a bug request in new library. \n\nThis library help you to use perform MongoDB based queries just like active record in CodeIgniter.\n\nThis library support\n* Write Concern and Journal\n* Read Preference\n* Aggregation Framework\n* Query Profiling\n\nWiki and how-to can be found here http://intekhabrizvi.github.io/mongodb-library-for-codeigniter.html \n\nFor Code Unitesting use branch named `unitesting`\n# Methods\n\n## Insert Method\n* `insert` Insert a new document into a collection\n* `batch_insert` Insert multiple new documents into a collection\n\n## Select Method\n* `select` Get select fields from returned documents\n* `where` OR `get_where` Where section of the query\n* `where_in` Where something is in an array of something\n* `where_in_all` Where something is in all of an array of * something\n* `where_not_in` Where something is not in array of something\n* `where_or` Where something is based on or\n* `where_gt` Where something is greater than something\n* `where_gte` Where something is greater than or equal to something\n* `where_lt` Where something is less than something\n* `where_lte` Where something is less than or equal to something\n* `where_between` Where something is in between to something\n* `where_between_ne` Where something is in between and but not equal to something\n* `where_ne` Where something is not equal to something\n* `like` Where something is search by like query\n* `order_by` Order the results\n* `limit` OR `offset` Limit the number of returned results\n* `count` Document Count based on where query\n* `distinct` Retrieve a list of distinct values for the given key across a single collection\n* `find_one` Retrieve single document from collection\n\n## Update Method\n* `set` Sets a field to a value\n* `unset_field` Unsets a field\n* `addtoset` Adds a value to an array if doesn't exist\n* `push` Pushes a value into an array field\n* `pop` Pops a value from an array field\n* `pull` Removes an array by the value of a field\n* `rename_field` Rename a field\n* `inc` Increments the value of a field\n* `mul` Multiple the value of a field\n* `max` Updates the value of the field to a specified value if the specified value is greater than the current value of the field\n* `min` Updates the value of the field to a specified value if the specified value is less than the current value of the field.\n* `update` Update a single document in a collection\n* `update_all` Update all documents in a collection\n\n## Delete Method\n* `delete` Delete a single document in a collection\n* `delete_all` Delete all documents in a collection\n\n## Aggregation Method\n* `aggregate` Perform aggregation query on document\n\n## Profiling Methods\n* `output_benchmark` return complete explain data for all the find based query performed\n\n## Index Method\n* `add_index` Create a new index on collection\n* `remove_index` Remove index from collection\n* `list_indexes` Show all index created on collections\n\n## DB Method\n* `switch_db` Switch to a different database\n* `drop_db` Drops a database\n* `drop_collection` Drops a collection\n* `command` Perform MongoDB command\n\n## Extra Helper\n* `date` Create or convert date to MongoDB based Date\n\n## License \nCreative Commons Attribution 3.0 License.\nCodes are provided AS IS basis, i am not responsible for anything.\n"
  },
  {
    "path": "application/config/mongo_db.php",
    "content": "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n/*\n| -------------------------------------------------------------------------\n| DATABASE CONNECTIVITY SETTINGS\n| -------------------------------------------------------------------------\n| This file will contain the settings needed to access your Mongo database.\n|\n|\n| ------------------------------------------------------------------------\n| EXPLANATION OF VARIABLES\n| ------------------------------------------------------------------------\n|\n|\t['hostname'] The hostname of your database server.\n|\t['username'] The username used to connect to the database\n|\t['password'] The password used to connect to the database\n|\t['database'] The name of the database you want to connect to\n|\t['db_debug'] TRUE/FALSE - Whether database errors should be displayed.\n|\t['write_concerns'] Default is 1: acknowledge write operations.  ref(http://php.net/manual/en/mongo.writeconcerns.php)\n|\t['journal'] Default is TRUE : journal flushed to disk. ref(http://php.net/manual/en/mongo.writeconcerns.php)\n|\t['read_preference'] Set the read preference for this connection. ref (http://php.net/manual/en/mongoclient.setreadpreference.php)\n|\t['read_preference_tags'] Set the read preference for this connection.  ref (http://php.net/manual/en/mongoclient.setreadpreference.php)\n|\n| The $config['mongo_db']['active'] variable lets you choose which connection group to\n| make active.  By default there is only one group (the 'default' group).\n|\n*/\n\n$config['mongo_db']['active'] = 'default';\n\n$config['mongo_db']['default']['no_auth'] = FALSE;\n$config['mongo_db']['default']['hostname'] = 'localhost';\n$config['mongo_db']['default']['port'] = '27017';\n$config['mongo_db']['default']['username'] = 'username';\n$config['mongo_db']['default']['password'] = 'password';\n$config['mongo_db']['default']['database'] = 'database';\n$config['mongo_db']['default']['db_debug'] = TRUE;\n$config['mongo_db']['default']['return_as'] = 'array';\n$config['mongo_db']['default']['write_concerns'] = (int)1;\n$config['mongo_db']['default']['journal'] = TRUE;\n$config['mongo_db']['default']['read_preference'] = NULL;\n$config['mongo_db']['default']['read_preference_tags'] = NULL;\n\n$config['mongo_db']['test']['no_auth'] = FALSE;\n$config['mongo_db']['test']['hostname'] = 'localhost';\n$config['mongo_db']['test']['port'] = '27017';\n$config['mongo_db']['test']['username'] = 'username';\n$config['mongo_db']['test']['password'] = 'password';\n$config['mongo_db']['test']['database'] = 'database';\n$config['mongo_db']['test']['db_debug'] = TRUE;\n$config['mongo_db']['test']['return_as'] = 'array';\n$config['mongo_db']['test']['write_concerns'] = (int)1;\n$config['mongo_db']['test']['journal'] = TRUE;\n$config['mongo_db']['test']['read_preference'] = NULL;\n$config['mongo_db']['test']['read_preference_tags'] = NULL;\n\n/* End of file database.php */\n/* Location: ./application/config/database.php */\n"
  },
  {
    "path": "application/libraries/Mongo_db.php",
    "content": "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\n/**\n* CodeIgniter MongoDB Active Record Library\n*\n* A library to interface with the NoSQL database MongoDB. For more information see http://www.mongodb.org\n*\n* @package CodeIgniter\n* @author Intekhab Rizvi | www.intekhab.in | me@intekhab.in\n* @copyright Copyright (c) 2014, Intekhab Rizvi.\n* @license http://www.opensource.org/licenses/mit-license.php\n* @link http://intekhab.in\n* @version Version 1.0\n* Thanks to Alex Bilbie (http://alexbilbie.com) for help.\n*/\n\nClass Mongo_db{\n\n\tprivate $CI;\n\tprivate $config = array();\n\tprivate $param = array();\n\tprivate $activate;\n\tprivate $connect;\n\tprivate $db;\n\tprivate $hostname;\n\tprivate $port;\n\tprivate $database;\n\tprivate $username;\n\tprivate $password;\n\tprivate $debug;\n\tprivate $write_concerns;\n\tprivate $journal;\n\tprivate $selects = array();\n\tprivate $updates = array();\n\tprivate $wheres\t= array();\n\tprivate $limit\t= 999999;\n\tprivate $offset\t= 0;\n\tprivate $sorts\t= array();\n\tprivate $return_as = 'array';\n\tpublic $benchmark = array();\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Class Constructor\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Automatically check if the Mongo PECL extension has been installed/enabled.\n\t* Get Access to all CodeIgniter available resources.\n\t* Load mongodb config file from application/config folder.\n\t* Prepare the connection variables and establish a connection to the MongoDB.\n\t* Try to connect on MongoDB server.\n\t*/\n\n\tfunction __construct($param)\n\t{\n\n\t\tif ( ! class_exists('Mongo') && ! class_exists('MongoClient'))\n\t\t{\n\t\t\tshow_error(\"The MongoDB PECL extension has not been installed or enabled\", 500);\n\t\t}\n\t\t$this->CI =& get_instance();\n\t\t$this->CI->load->config('mongo_db');\n\t\t$this->config = $this->CI->config->item('mongo_db');\n\t\t$this->param = $param;\n\t\t$this->connect();\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Class Destructor\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Close all open connections.\n\t*/\n\tfunction __destruct()\n\t{\n\t\tif(is_object($this->connect))\n\t\t{\n\t\t\t$this->connect->close();\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Prepare configuration for mongoDB connection\n\t* --------------------------------------------------------------------------------\n\t* \n\t* Validate group name or autoload default group name from config file.\n\t* Validate all the properties present in config file of the group.\n\t*/\n\n\tprivate function prepare()\n\t{\n\t\tif(is_array($this->param) && count($this->param) > 0 && isset($this->param['activate']) == TRUE)\n\t\t{\n\t\t\t$this->activate = $this->param['activate'];\n\t\t}\n\t\telse if(isset($this->config['active']) && !empty($this->config['active']))\n\t\t{\n\t\t\t$this->activate = $this->config['active'];\n\t\t}else\n\t\t{\n\t\t\tshow_error(\"MongoDB configuration is missing.\", 500);\n\t\t}\n\n\t\tif(isset($this->config[$this->activate]) == TRUE)\n\t\t{\n\t\t\tif(empty($this->config[$this->activate]['hostname']))\n\t\t\t{\n\t\t\t\tshow_error(\"Hostname missing from mongodb config group : {$this->activate}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->hostname = trim($this->config[$this->activate]['hostname']);\n\t\t\t}\n\n\t\t\tif(empty($this->config[$this->activate]['port']))\n\t\t\t{\n\t\t\t\tshow_error(\"Port number missing from mongodb config group : {$this->activate}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->port = trim($this->config[$this->activate]['port']);\n\t\t\t}\n\n\t\t\tif(isset($this->config[$this->activate]['no_auth']) == FALSE\n\t\t\t   && empty($this->config[$this->activate]['username']))\n\t\t\t{\n\t\t\t\tshow_error(\"Username missing from mongodb config group : {$this->activate}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->username = trim($this->config[$this->activate]['username']);\n\t\t\t}\n\n\t\t\tif(isset($this->config[$this->activate]['no_auth']) == FALSE \n\t\t\t   && empty($this->config[$this->activate]['password']))\n\t\t\t{\n\t\t\t\tshow_error(\"Password missing from mongodb config group : {$this->activate}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->password = trim($this->config[$this->activate]['password']);\n\t\t\t}\n\n\t\t\tif(empty($this->config[$this->activate]['database']))\n\t\t\t{\n\t\t\t\tshow_error(\"Database name missing from mongodb config group : {$this->activate}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->database = trim($this->config[$this->activate]['database']);\n\t\t\t}\n\n\t\t\tif(empty($this->config[$this->activate]['db_debug']))\n\t\t\t{\n\t\t\t\t$this->debug = FALSE;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->debug = $this->config[$this->activate]['db_debug'];\n\t\t\t}\n\n\t\t\tif(empty($this->config[$this->activate]['write_concerns']))\n\t\t\t{\n\t\t\t\t$this->write_concerns = 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->write_concerns = $this->config[$this->activate]['write_concerns'];\n\t\t\t}\n\n\t\t\tif(empty($this->config[$this->activate]['journal']))\n\t\t\t{\n\t\t\t\t$this->journal = TRUE;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->journal = $this->config[$this->activate]['journal'];\n\t\t\t}\n\n\t\t\tif(empty($this->config[$this->activate]['return_as']))\n\t\t\t{\n\t\t\t\t$this->return_as = 'array';\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->return_as = $this->config[$this->activate]['return_as'];\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tshow_error(\"mongodb config group :  <strong>{$this->activate}</strong> does not exist.\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Connect to MongoDB Database\n\t* --------------------------------------------------------------------------------\n\t* \n\t* Connect to mongoDB database or throw exception with the error message.\n\t*/\n\n\tprivate function connect()\n\t{\n\t\t$this->prepare();\n\t\ttry\n\t\t{\n\t\t\t$dns = \"mongodb://{$this->hostname}:{$this->port}/{$this->database}\";\n\t\t\tif(isset($this->config[$this->activate]['no_auth']) == TRUE && $this->config[$this->activate]['no_auth'] == TRUE)\n\t\t\t{\n\t\t\t\t$options = array();\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$options = array('username'=>$this->username, 'password'=>$this->password);\n\t\t\t}\n\t\t\t$this->connect = new MongoClient($dns, $options);\n\t\t\t$this->db = $this->connect->selectDB($this->database);\n\t\t\t$this->db = $this->connect->{$this->database};\n\t\t}\n\t\tcatch (MongoConnectionException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Unable to connect to MongoDB: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Unable to connect to MongoDB\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Insert\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Insert a new document into the passed collection\n\t*\n\t* @usage : $this->mongo_db->insert('foo', $data = array());\n\t*/\n\tpublic function insert($collection = \"\", $insert = array())\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected to insert into\", 500);\n\t\t}\n\n\t\tif (!is_array($insert) || count($insert) == 0)\n\t\t{\n\t\t\tshow_error(\"Nothing to insert into Mongo collection or insert is not an array\", 500);\n\t\t}\n\n\t\ttry\n\t\t{\n\t\t\t$this->db->{$collection}->insert($insert, array('w' => $this->write_concerns, 'j'=>$this->journal));\n\t\t\tif (isset($insert['_id']))\n\t\t\t{\n\t\t\t\treturn ($insert['_id']);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn (FALSE);\n\t\t\t}\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Insert of data into MongoDB failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Insert of data into MongoDB failed\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Batch Insert\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Insert a multiple document into the collection\n\t*\n\t* @usage : $this->mongo_db->batch_insert('foo', $data = array());\n\t* @return : bool or array : if query fail then false else array of _id successfully inserted.\n\t*/\n\tpublic function batch_insert($collection = \"\", $insert = array())\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected to insert into\", 500);\n\t\t}\n\t\tif (count($insert) == 0 || !is_array($insert))\n\t\t{\n\t\t\tshow_error(\"Nothing to insert into Mongo collection or insert is not an array\", 500);\n\t\t}\n\t\ttry\n\t\t{\n\t\t\t$this->db->{$collection}->batchInsert($insert, array('w' => $this->write_concerns, 'j'=>$this->journal));\n\t\t\tif(is_array($insert) && count($insert) > 0)\n\t\t\t{\n\t\t\t    $insert_ids = array_map(function ($arr) {return $arr['_id'];},$insert);\n\t\t\t    return ($insert_ids);\n\t\t\t}\n\t\t\telse \n\t\t\t{\n\t\t\t    return (FALSE);\n\t\t\t}\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Batch insert of data into MongoDB failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Batch insert of data into MongoDB failed\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Select\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Determine which fields to include OR which to exclude during the query process.\n\t* If you want to only choose fields to exclude, leave $includes an empty array().\n\t*\n\t* @usage: $this->mongo_db->select(array('foo', 'bar'))->get('foobar');\n\t*/\n\tpublic function select($includes = array(), $excludes = array())\n\t{\n\t\tif ( ! is_array($includes))\n\t\t{\n\t\t\t$includes = array();\n\t\t}\n\t\tif ( ! is_array($excludes))\n\t\t{\n\t\t\t$excludes = array();\n\t\t}\n\t\tif ( ! empty($includes))\n\t\t{\n\t\t\tforeach ($includes as $key=> $col)\n\t\t\t{\n\t\t\t\tif(is_array($col)){\n\t\t\t\t\t//support $elemMatch in select\n\t\t\t\t\t$this->selects[$key] = $col;\n\t\t\t\t}else{\n\t\t\t\t\t$this->selects[$col] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif ( ! empty($excludes))\n\t\t{\n\t\t\tforeach ($excludes as $col)\n\t\t\t{\n\t\t\t\t$this->selects[$col] = 0;\n\t\t\t}\n\t\t}\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Where\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents based on these search parameters. The $wheres array should\n\t* be an associative array with the field as the key and the value as the search\n\t* criteria.\n\t*\n\t* @usage : $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');\n\t*/\n\tpublic function where($wheres, $value = null)\n\t{\n\t\tif (is_array($wheres))\n\t\t{\n\t\t\tforeach ($wheres as $wh => $val)\n\t\t\t{\n\t\t\t\t$this->wheres[$wh] = $val;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\t$this->wheres[$wheres] = $value;\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* or where\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field may be something else\n\t*\n\t* @usage : $this->mongo_db->where_or(array('foo'=>'bar', 'bar'=>'foo'))->get('foobar');\n\t*/\n\tpublic function where_or($wheres = array())\n\t{\n\t\tif (is_array($wheres) && count($wheres) > 0)\n\t\t{\n\t\t\tif ( ! isset($this->wheres['$or']) || ! is_array($this->wheres['$or']))\n\t\t\t{\n\t\t\t\t$this->wheres['$or'] = array();\n\t\t\t}\n\t\t\tforeach ($wheres as $wh => $val)\n\t\t\t{\n\t\t\t\t$this->wheres['$or'][] = array($wh=>$val);\n\t\t\t}\n\t\t\treturn ($this);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tshow_error(\"Where value should be an array.\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where in\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is in a given $in array().\n\t*\n\t* @usage : $this->mongo_db->where_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');\n\t*/\n\tpublic function where_in($field = \"\", $in = array())\n\t{\n\t\tif (empty($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform where in query.\", 500);\n\t\t}\n\n\t\tif (is_array($in) && count($in) > 0)\n\t\t{\n\t\t\t$this->_w($field);\n\t\t\t$this->wheres[$field]['$in'] = $in;\n\t\t\treturn ($this);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tshow_error(\"in value should be an array.\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where in all\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is in all of a given $in array().\n\t*\n\t* @usage : $this->mongo_db->where_in_all('foo', array('bar', 'zoo', 'blah'))->get('foobar');\n\t*/\n\tpublic function where_in_all($field = \"\", $in = array())\n\t{\n\t\tif (empty($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform where all in query.\", 500);\n\t\t}\n\n\t\tif (is_array($in) && count($in) > 0)\n\t\t{\n\t\t\t$this->_w($field);\n\t\t\t$this->wheres[$field]['$all'] = $in;\n\t\t\treturn ($this);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tshow_error(\"in value should be an array.\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where not in\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is not in a given $in array().\n\t*\n\t* @usage : $this->mongo_db->where_not_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');\n\t*/\n\tpublic function where_not_in($field = \"\", $in = array())\n\t{\n\t\tif (empty($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform where not in query.\", 500);\n\t\t}\n\n\t\tif (is_array($in) && count($in) > 0)\n\t\t{\n\t\t\t$this->_w($field);\n\t\t\t$this->wheres[$field]['$nin'] = $in;\n\t\t\treturn ($this);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tshow_error(\"in value should be an array.\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where greater than\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is greater than $x\n\t*\n\t* @usage : $this->mongo_db->where_gt('foo', 20);\n\t*/\n\tpublic function where_gt($field = \"\", $x)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform greater then query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's value is require to perform greater then query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$gt'] = $x;\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where greater than or equal to\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is greater than or equal to $x\n\t*\n\t* @usage : $this->mongo_db->where_gte('foo', 20);\n\t*/\n\tpublic function where_gte($field = \"\", $x)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform greater then or equal query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's value is require to perform greater then or equal query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$gte'] = $x;\n\t\treturn($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where less than\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is less than $x\n\t*\n\t* @usage : $this->mongo_db->where_lt('foo', 20);\n\t*/\n\tpublic function where_lt($field = \"\", $x)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform less then query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's value is require to perform less then query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$lt'] = $x;\n\t\treturn($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where less than or equal to\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is less than or equal to $x\n\t*\n\t* @usage : $this->mongo_db->where_lte('foo', 20);\n\t*/\n\tpublic function where_lte($field = \"\", $x)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform less then or equal to query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's value is require to perform less then or equal to query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$lte'] = $x;\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where between\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is between $x and $y\n\t*\n\t* @usage : $this->mongo_db->where_between('foo', 20, 30);\n\t*/\n\tpublic function where_between($field = \"\", $x, $y)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform greater then or equal to query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's start value is require to perform greater then or equal to query.\", 500);\n\t\t}\n\n\t\tif (!isset($y))\n\t\t{\n\t\t\tshow_error(\"Mongo field's end value is require to perform greater then or equal to query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$gte'] = $x;\n\t\t$this->wheres[$field]['$lte'] = $y;\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where between and but not equal to\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is between but not equal to $x and $y\n\t*\n\t* @usage : $this->mongo_db->where_between_ne('foo', 20, 30);\n\t*/\n\tpublic function where_between_ne($field = \"\", $x, $y)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform between and but not equal to query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's start value is require to perform between and but not equal to query.\", 500);\n\t\t}\n\n\t\tif (!isset($y))\n\t\t{\n\t\t\tshow_error(\"Mongo field's end value is require to perform between and but not equal to query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$gt'] = $x;\n\t\t$this->wheres[$field]['$lt'] = $y;\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where not equal\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the value of a $field is not equal to $x\n\t*\n\t* @usage : $this->mongo_db->where_ne('foo', 1)->get('foobar');\n\t*/\n\tpublic function where_ne($field = '', $x)\n\t{\n\t\tif (!isset($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform Where not equal to query.\", 500);\n\t\t}\n\n\t\tif (!isset($x))\n\t\t{\n\t\t\tshow_error(\"Mongo field's value is require to perform Where not equal to query.\", 500);\n\t\t}\n\n\t\t$this->_w($field);\n\t\t$this->wheres[$field]['$ne'] = $x;\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Like\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents where the (string) value of a $field is like a value. The defaults\n\t* allow for a case-insensitive search.\n\t*\n\t* @param $flags\n\t* Allows for the typical regular expression flags:\n\t* i = case insensitive\n\t* m = multiline\n\t* x = can contain comments\n\t* l = locale\n\t* s = dotall, \".\" matches everything, including newlines\n\t* u = match unicode\n\t*\n\t* @param $enable_start_wildcard\n\t* If set to anything other than TRUE, a starting line character \"^\" will be prepended\n\t* to the search value, representing only searching for a value at the start of\n\t* a new line.\n\t*\n\t* @param $enable_end_wildcard\n\t* If set to anything other than TRUE, an ending line character \"$\" will be appended\n\t* to the search value, representing only searching for a value at the end of\n\t* a line.\n\t*\n\t* @usage : $this->mongo_db->like('foo', 'bar', 'im', FALSE, TRUE);\n\t*/\n\tpublic function like($field = \"\", $value = \"\", $flags = \"i\", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE)\n\t{\n\t\tif (empty($field))\n\t\t{\n\t\t\tshow_error(\"Mongo field is require to perform like query.\", 500);\n\t\t}\n\n\t\tif (empty($value))\n\t\t{\n\t\t\tshow_error(\"Mongo field's value is require to like query.\", 500);\n\t\t}\n\n\t\t$field = (string) trim($field);\n\t\t$this->_w($field);\n\t\t$value = (string) trim($value);\n\t\t$value = quotemeta($value);\n\t\tif ($enable_start_wildcard !== TRUE)\n\t\t{\n\t\t\t$value = \"^\" . $value;\n\t\t}\n\t\tif ($enable_end_wildcard !== TRUE)\n\t\t{\n\t\t\t$value .= \"$\";\n\t\t}\n\t\t$regex = \"/$value/$flags\";\n\t\t$this->wheres[$field] = new MongoRegex($regex);\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Get\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents based upon the passed parameters\n\t*\n\t* @usage : $this->mongo_db->get('foo');\n\t*/\n\tpublic function get($collection = \"\")\n\t{\t\t\t\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"In order to retrieve documents from MongoDB, a collection name must be passed\", 500);\n\t\t}\n\t\ttry{\t\n\t\t\t$documents = $this->db->{$collection}\n\t\t\t->find($this->wheres, $this->selects)\n\t\t\t->limit((int) $this->limit)\n\t\t\t->skip((int) $this->offset)\n\t\t\t->sort($this->sorts);\n\t\t\t$this->explain($documents, $collection);\n\t\t\t// Clear\n\t\t\t$this->_clear();\n\t\t\t$returns = array();\n\t\t\t\n\t\t\twhile ($documents->hasNext())\n\t\t\t{\n\t\t\t\tif ($this->return_as == 'object')\n\t\t\t\t{\n\t\t\t\t\t$returns[] = (object) $documents->getNext();\t\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t$returns[] = (array) $documents->getNext();\n\t\t\t\t}\n\t\t\t}\n\t\t\tif ($this->return_as == 'object')\n\t\t\t{\n\t\t\t\treturn (object)$returns;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn $returns;\n\t\t\t}\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"MongoDB query failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"MongoDB query failed.\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Get where\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the documents based upon the passed parameters\n\t*\n\t* @usage : $this->mongo_db->get_where('foo', array('bar' => 'something'));\n\t*/\n\tpublic function get_where($collection = \"\", $where = array())\n\t{\n\t\tif (is_array($where) && count($where) > 0)\n\t\t{\n\t\t\treturn $this->where($where)\n\t\t\t->get($collection);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tshow_error(\"Nothing passed to perform search or value is empty.\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Find One\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Get the single document based upon the passed parameters\n\t*\n\t* @usage : $this->mongo_db->find_one('foo');\n\t*/\n\tpublic function find_one($collection = \"\")\n\t{\n\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"In order to retrieve documents from MongoDB, a collection name must be passed\", 500);\n\t\t}\n\n\t\ttry{\n\n\t\t\t$document = $this->db->{$collection}->findOne($this->wheres, $this->selects);\n\t\t\t// Clear\n\t\t\t$this->_clear();\n\t\t\tif(is_null($document))\n\t\t\t{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif ($this->return_as == 'object')\n\t\t\t\t{\n\t\t\t\t\treturn (object)$document;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\treturn $document;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"MongoDB query failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"MongoDB query failed.\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Count\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Count the documents based upon the passed parameters\n\t*\n\t* @usage : $this->mongo_db->count('foo');\n\t*/\n\tpublic function count($collection = \"\") \n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"In order to retrieve a count of documents from MongoDB, a collection name must be passed\", 500);\n\t\t}\n\t\t$count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)->skip((int) $this->offset)->count();\n\t\t$this->_clear();\n\t\treturn ($count);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Set\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Sets a field to a value\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->set('posted', 1)->update('blog_posts');\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->set(array('posted' => 1, 'time' => time()))->update('blog_posts');\n\t*/\n\tpublic function set($fields, $value = NULL)\n\t{\n\t\t$this->_u('$set');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$set'][$fields] = $value;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field => $value)\n\t\t\t{\n\t\t\t$this->updates['$set'][$field] = $value;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Unset\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Unsets a field (or fields)\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->unset('posted')->update('blog_posts');\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->set(array('posted','time'))->update('blog_posts');\n\t*/\n\tpublic function unset_field($fields)\n\t{\n\t\t$this->_u('$unset');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$unset'][$fields] = 1;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field)\n\t\t\t{\n\t\t\t\t$this->updates['$unset'][$field] = 1;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Add to set\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Adds value to the array only if its not in the array already\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->addtoset('tags', 'php')->update('blog_posts');\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->addtoset('tags', array('php', 'codeigniter', 'mongodb'))->update('blog_posts');\n\t*/\n\tpublic function addtoset($field, $values)\n\t{\n\t\t$this->_u('$addToSet');\n\t\tif (is_string($values))\n\t\t{\n\t\t\t$this->updates['$addToSet'][$field] = $values;\n\t\t}\n\t\telseif (is_array($values))\n\t\t{\n\t\t\t$this->updates['$addToSet'][$field] = array('$each' => $values);\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Push\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Pushes values into a field (field must be an array)\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->push('comments', array('text'=>'Hello world'))->update('blog_posts');\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->push(array('comments' => array('text'=>'Hello world')), 'viewed_by' => array('Alex')->update('blog_posts');\n\t*/\n\tpublic function push($fields, $value = array())\n\t{\n\t\t$this->_u('$push');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$push'][$fields] = $value;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field => $value)\n\t\t\t{\n\t\t\t\t$this->updates['$push'][$field] = $value;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Pop\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Pops the last value from a field (field must be an array)\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->pop('comments')->update('blog_posts');\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->pop(array('comments', 'viewed_by'))->update('blog_posts');\n\t*/\n\tpublic function pop($field)\n\t{\n\t\t$this->_u('$pop');\n\t\tif (is_string($field))\n\t\t{\n\t\t\t$this->updates['$pop'][$field] = -1;\n\t\t}\n\t\telseif (is_array($field))\n\t\t{\n\t\t\tforeach ($field as $pop_field)\n\t\t\t{\n\t\t\t\t$this->updates['$pop'][$pop_field] = -1;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Pull\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Removes by an array by the value of a field\n\t*\n\t* @usage: $this->mongo_db->pull('comments', array('comment_id'=>123))->update('blog_posts');\n\t*/\n\tpublic function pull($field = \"\", $value = array())\n\t{\n\t\t$this->_u('$pull');\n\t\t$this->updates['$pull'] = array($field => $value);\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Rename field\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Renames a field\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->rename_field('posted_by', 'author')->update('blog_posts');\n\t*/\n\tpublic function rename_field($old, $new)\n\t{\n\t\t$this->_u('$rename');\n\t\t$this->updates['$rename'] = array($old => $new);\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Inc\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Increments the value of a field\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->inc(array('num_comments' => 1))->update('blog_posts');\n\t*/\n\tpublic function inc($fields = array(), $value = 0)\n\t{\n\t\t$this->_u('$inc');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$inc'][$fields] = $value;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field => $value)\n\t\t\t{\n\t\t\t\t$this->updates['$inc'][$field] = $value;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Multiple\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Multiple the value of a field\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->mul(array('num_comments' => 3))->update('blog_posts');\n\t*/\n\tpublic function mul($fields = array(), $value = 0)\n\t{\n\t\t$this->_u('$mul');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$mul'][$fields] = $value;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field => $value)\n\t\t\t{\n\t\t\t\t$this->updates['$mul'][$field] = $value;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Maximum\n\t* --------------------------------------------------------------------------------\n\t*\n\t* The $max operator updates the value of the field to a specified value if the specified value is greater than the current value of the field.\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->max(array('num_comments' => 3))->update('blog_posts');\n\t*/\n\tpublic function max($fields = array(), $value = 0)\n\t{\n\t\t$this->_u('$max');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$max'][$fields] = $value;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field => $value)\n\t\t\t{\n\t\t\t\t$this->updates['$max'][$field] = $value;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Minimum\n\t* --------------------------------------------------------------------------------\n\t*\n\t* The $min updates the value of the field to a specified value if the specified value is less than the current value of the field.\n\t*\n\t* @usage: $this->mongo_db->where(array('blog_id'=>123))->min(array('num_comments' => 3))->update('blog_posts');\n\t*/\n\tpublic function min($fields = array(), $value = 0)\n\t{\n\t\t$this->_u('$min');\n\t\tif (is_string($fields))\n\t\t{\n\t\t\t$this->updates['$min'][$fields] = $value;\n\t\t}\n\t\telseif (is_array($fields))\n\t\t{\n\t\t\tforeach ($fields as $field => $value)\n\t\t\t{\n\t\t\t\t$this->updates['$min'][$field] = $value;\n\t\t\t}\n\t\t}\n\t\treturn $this;\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! distinct\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Finds the distinct values for a specified field across a single collection\n\t*\n\t* @usage: $this->mongo_db->distinct('collection', 'field');\n\t*/\n\tpublic function distinct($collection = \"\", $field=\"\")\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected for update\", 500);\n\t\t}\n\n\t\tif (empty($field))\n\t\t{\n\t\t\tshow_error(\"Need Collection field information for performing distinct query\", 500);\n\t\t}\n\n\t\ttry\n\t\t{\n\t\t\t$documents = $this->db->{$collection}->distinct($field, $this->wheres);\n\t\t\t$this->_clear();\n\t\t\tif ($this->return_as == 'object')\n\t\t\t{\n\t\t\t\treturn (object)$documents;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn $documents;\n\t\t\t}\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"MongoDB Distinct Query Failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"MongoDB failed\", 500);\n\t\t\t}\n\t\t}\n\t}\t\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Update\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Updates a single document in Mongo\n\t*\n\t* @usage: $this->mongo_db->update('foo', $data = array());\n\t*/\n\tpublic function update($collection = \"\", $options = array())\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected for update\", 500);\n\t\t}\n\n\t\ttry\n\t\t{\n\t\t\t$options = array_merge(array('w' => $this->write_concerns, 'j'=>$this->journal, 'multiple' => FALSE), $options);\n\t\t\t$this->db->{$collection}->update($this->wheres, $this->updates, $options);\n\t\t\t$this->_clear();\n\t\t\treturn (TRUE);\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Update of data into MongoDB failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Update of data into MongoDB failed\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Update all\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Updates a collection of documents\n\t*\n\t* @usage: $this->mongo_db->update_all('foo', $data = array());\n\t*/\n\tpublic function update_all($collection = \"\", $data = array(), $options = array())\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected to update\", 500);\n\t\t}\n\t\tif (is_array($data) && count($data) > 0)\n\t\t{\n\t\t\t$this->updates = array_merge($data, $this->updates);\n\t\t}\n\t\tif (count($this->updates) == 0)\n\t\t{\n\t\t\tshow_error(\"Nothing to update in Mongo collection or update is not an array\", 500);\t\n\t\t}\n\t\ttry\n\t\t{\n\t\t\t$options = array_merge(array('w' => $this->write_concerns, 'j'=>$this->journal, 'multiple' => TRUE), $options);\n\t\t\t$this->db->{$collection}->update($this->wheres, $this->updates, $options);\n\t\t\t$this->_clear();\n\t\t\treturn (TRUE);\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Update of data into MongoDB failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Update of data into MongoDB failed\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Delete\n\t* --------------------------------------------------------------------------------\n\t*\n\t* delete document from the passed collection based upon certain criteria\n\t*\n\t* @usage : $this->mongo_db->delete('foo');\n\t*/\n\tpublic function delete($collection = \"\")\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected to delete from\", 500);\n\t\t}\n\t\ttry\n\t\t{\n\t\t\t$this->db->{$collection}->remove($this->wheres, array('w' => $this->write_concerns, 'j'=>$this->journal, 'justOne' => TRUE));\n\t\t\t$this->_clear();\n\t\t\treturn (TRUE);\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Delete of data into MongoDB failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Delete of data into MongoDB failed\", 500);\n\t\t\t}\n\t\t\t\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Delete all\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Delete all documents from the passed collection based upon certain criteria\n\t*\n\t* @usage : $this->mongo_db->delete_all('foo', $data = array());\n\t*/\n\tpublic function delete_all($collection = \"\")\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection selected to delete from\", 500);\n\t\t}\n\t\t/*if (isset($this->wheres['_id']) and ! is_object($this->wheres['_id']))\n\t\t{\n\t\t\t$this->wheres['_id'] = new MongoId($this->wheres['_id']);\n\t\t}*/\n\t\ttry\n\t\t{\n\t\t\t$this->db->{$collection}->remove($this->wheres, array('w' => $this->write_concerns, 'j'=>$this->journal, 'justOne' => FALSE));\n\t\t\t$this->_clear();\n\t\t\treturn (TRUE);\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Delete of data into MongoDB failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Delete of data into MongoDB failed\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Aggregation Operation\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Perform aggregation on mongodb collection\n\t*\n\t* @usage : $this->mongo_db->aggregate('foo', $ops = array());\n\t*/\n\tpublic function aggregate($collection, $operation)\n\t{\n        if (empty($collection))\n\t \t{\n\t \t\tshow_error(\"In order to retreive documents from MongoDB, a collection name must be passed\", 500);\n\t \t}\n \t\t\n \t\tif (empty($operation) && !is_array($operation))\n\t \t{\n\t \t\tshow_error(\"Operation must be an array to perform aggregate.\", 500);\n\t \t}\n\n\t \ttry\n\t \t{\n\t \t\t$documents = $this->db->{$collection}->aggregate($operation);\n\t \t\t$this->_clear();\n\t \t\tif ($this->return_as == 'object')\n\t\t\t{\n\t\t\t\treturn (object)$documents;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn $documents;\n\t\t\t}\n\t \t}\n\t \tcatch (MongoResultException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Aggregation operation failed: {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Aggregation operation failed.\", 500);\n\t\t\t}\n\t\t}\n    }\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Order by\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Sort the documents based on the parameters passed. To set values to descending order,\n\t* you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be\n\t* set to 1 (ASC).\n\t*\n\t* @usage : $this->mongo_db->order_by(array('foo' => 'ASC'))->get('foobar');\n\t*/\n\tpublic function order_by($fields = array())\n\t{\n\t\tforeach ($fields as $col => $val)\n\t\t{\n\t\tif ($val == -1 || $val === FALSE || strtolower($val) == 'desc')\n\t\t\t{\n\t\t\t\t$this->sorts[$col] = -1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$this->sorts[$col] = 1;\n\t\t\t}\n\t\t}\n\t\treturn ($this);\n\t}\n\n\t /**\n\t* --------------------------------------------------------------------------------\n\t* Mongo Date\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Create new MongoDate object from current time or pass timestamp to create\n\t* mongodate.\n\t*\n\t* @usage : $this->mongo_db->date($timestamp);\n\t*/\n\tpublic function date($stamp = FALSE)\n\t{\n\t\tif ( $stamp == FALSE )\n\t\t{\n\t\t\treturn new MongoDate();\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn new MongoDate($stamp);\n\t\t}\n\t\t\n\t}\n\n\t /**\n\t* --------------------------------------------------------------------------------\n\t* Mongo Benchmark\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Output all benchmark data for all performed queries.\n\t*\n\t* @usage : $this->mongo_db->output_benchmark();\n\t*/\n\tpublic function output_benchmark()\n\t{\n\t\treturn $this->benchmark;\n\t}\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Limit results\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Limit the result set to $x number of documents\n\t*\n\t* @usage : $this->mongo_db->limit($x);\n\t*/\n\tpublic function limit($x = 99999)\n\t{\n\t\tif ($x !== NULL && is_numeric($x) && $x >= 1)\n\t\t{\n\t\t\t$this->limit = (int) $x;\n\t\t}\n\t\treturn ($this);\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Offset\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Offset the result set to skip $x number of documents\n\t*\n\t* @usage : $this->mongo_db->offset($x);\n\t*/\n\tpublic function offset($x = 0)\n\t{\n\t\tif ($x !== NULL && is_numeric($x) && $x >= 1)\n\t\t{\n\t\t\t$this->offset = (int) $x;\n\t\t}\n\t\treturn ($this);\n\t}\n\t\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* // Command\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Runs a MongoDB command\n\t*\n\t* @param  string : Collection name, array $query The command query\n\t* @usage : $this->mongo_db->command($collection, array('geoNear'=>'buildings', 'near'=>array(53.228482, -0.547847), 'num' => 10, 'nearSphere'=>true));\n\t* @access public\n        * @return object or array\n\t*/\n\t\n        public function command($collection, $command = array())\n        {\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection specified to run command\", 500);\n\t\t}\n\t\t\n\t\tif (empty($command) || ! is_array($command))\n\t\t{\n\t\t\tshow_error(\"no command were specified\", 500);\n\t\t}\n\t\t\n                try\n                {\n                        $returns = $this->db->{$collection}->command($command);\n\t\t\t\n                        if ($this->return_as == 'object')\n\t\t\t{\n\t\t\t\treturn (object)$returns;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn $returns;\n\t\t\t}\n                }\n\n                catch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Command failed : {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Command failed.\", 500);\n\t\t\t}\n\t\t}\n        }\n\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Add indexes\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Ensure an index of the keys in a collection with optional parameters. To set values to descending order,\n\t* you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be\n\t* set to 1 (ASC).\n\t*\n\t* @usage : $this->mongo_db->add_index($collection, array('first_name' => 'ASC', 'last_name' => -1), array('unique' => TRUE));\n\t*/\n\tpublic function add_index($collection = \"\", $keys = array(), $options = array())\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection specified to add index to\", 500);\n\t\t}\n\n\t\tif (empty($keys) || ! is_array($keys))\n\t\t{\n\t\t\tshow_error(\"Index could not be created to MongoDB Collection because no keys were specified\", 500);\n\t\t}\n\n\t\tforeach ($keys as $col => $val)\n\t\t{\n\t\t\tif($val == -1 || $val === FALSE || strtolower($val) == 'desc')\n\t\t\t{\n\t\t\t\t$keys[$col] = -1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$keys[$col] = 1;\n\t\t\t}\n\t\t}\n\t\ttry{\n\t\t\t$this->db->{$collection}->createIndex($keys, $options);\n\t\t\t$this->_clear();\n\t\t\treturn TRUE;\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Creating Index failed : {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Creating Index failed.\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Remove index\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Remove an index of the keys in a collection. To set values to descending order,\n\t* you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be\n\t* set to 1 (ASC).\n\t*\n\t* @usage : $this->mongo_db->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1));\n\t*/\n\tpublic function remove_index($collection = \"\", $keys = array())\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection specified to remove index from\", 500);\n\t\t}\n\n\t\tif (empty($keys) || ! is_array($keys))\n\t\t{\n\t\t\tshow_error(\"Index could not be removed from MongoDB Collection because no keys were specified\", 500);\n\t\t}\n\n\t\ttry\n\t\t{\t\n\t\t\t$this->db->{$collection}->deleteIndex($keys);\n\t\t\t$this->_clear();\n\t\t\treturn TRUE;\n\t\t}\n\t\tcatch (MongoCursorException $e)\n\t\t{\n\t\t\tif(isset($this->debug) == TRUE && $this->debug == TRUE)\n\t\t\t{\n\t\t\t\tshow_error(\"Creating Index failed : {$e->getMessage()}\", 500);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshow_error(\"Creating Index failed.\", 500);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* List indexes\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Lists all indexes in a collection.\n\t*\n\t* @usage : $this->mongo_db->list_indexes($collection);\n\t*/\n\tpublic function list_indexes($collection = \"\")\n\t{\n\t\tif (empty($collection))\n\t\t{\n\t\t\tshow_error(\"No Mongo collection specified to remove all indexes from\", 500);\n\t\t}\n\t\treturn ($this->db->{$collection}->getIndexInfo());\n\t}\t\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Switch database\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Switch from default database to a different db\n\t*\n\t* $this->mongo_db->switch_db('foobar');\n\t*/\n\tpublic function switch_db($database = '')\n\t{\n\t\tif (empty($database))\n\t\t{\n\t\t\tshow_error(\"To switch MongoDB databases, a new database name must be specified\", 500);\n\t\t}\n\n\t\t$this->database = $database;\n\n\t\ttry\n\t\t{\n\t\t\t$this->db = $this->connect->{$this->database};\n\t\t\treturn (TRUE);\n\t\t}\n\t\tcatch (Exception $e)\n\t\t{\n\t\t\tshow_error(\"Unable to switch Mongo Databases: {$e->getMessage()}\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Drop database\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Drop a Mongo database\n\t* @usage: $this->mongo_db->drop_db(\"foobar\");\n\t*/\n\tpublic function drop_db($database = '')\n\t{\n\t\tif (empty($database))\n\t\t{\n\t\t\tshow_error('Failed to drop MongoDB database because name is empty', 500);\n\t\t}\n\n\t\ttry\n\t\t{\n\t\t\t$this->connect->{$database}->drop();\n\t\t\treturn (TRUE);\n\t\t}\n\t\tcatch (Exception $e)\n\t\t{\n\t\t\tshow_error(\"Unable to drop Mongo database `{$database}`: {$e->getMessage()}\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* //! Drop collection\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Drop a Mongo collection\n\t* @usage: $this->mongo_db->drop_collection('bar');\n\t*/\n\tpublic function drop_collection($col = '')\n\t{\n\t\tif (empty($col))\n\t\t{\n\t\t\tshow_error('Failed to drop MongoDB collection because collection name is empty', 500);\n\t\t}\n\n\t\ttry\n\t\t{\n\t\t\t$this->db->{$col}->drop();\n\t\t\treturn TRUE;\n\t\t}\n\t\tcatch (Exception $e)\n\t\t{\n\t\t\tshow_error(\"Unable to drop Mongo collection `{$col}`: {$e->getMessage()}\", 500);\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* _clear\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Resets the class variables to default settings\n\t*/\n\tprivate function _clear()\n\t{\n\t\t$this->selects\t= array();\n\t\t$this->updates\t= array();\n\t\t$this->wheres\t= array();\n\t\t$this->limit\t= 999999;\n\t\t$this->offset\t= 0;\n\t\t$this->sorts\t= array();\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Where initializer\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Prepares parameters for insertion in $wheres array().\n\t*/\n\tprivate function _w($param)\n\t{\n\t\tif ( ! isset($this->wheres[$param]))\n\t\t{\n\t\t\t$this->wheres[ $param ] = array();\n\t\t}\n\t}\n\n\t/**\n\t* --------------------------------------------------------------------------------\n\t* Update initializer\n\t* --------------------------------------------------------------------------------\n\t*\n\t* Prepares parameters for insertion in $updates array().\n\t*/\n\tprivate function _u($method)\n\t{\n\t\tif ( ! isset($this->updates[$method]))\n\t\t{\n\t\t\t$this->updates[ $method ] = array();\n\t\t}\n\t}\n\n\tprivate function explain($cursor, $collection, $aggregate=null)\n\t{\n\t\tarray_push($this->benchmark, \n\t\t\tarray(\n\t\t\t\t\t'benchmark'=>$cursor->explain(),\n\t\t\t\t\t'query'=> array(\n\t\t\t\t\t\t\t'collection'=>$collection, \n\t\t\t\t\t\t\t'select'=>$this->selects,\n\t\t\t\t\t\t\t'update'=>$this->updates, \n\t\t\t\t\t\t\t'where'=>$this->wheres, \n\t\t\t\t\t\t\t'sort'=>$this->sorts)\n\t\t\t\t)\n\t\t);\n\t}\n}\n"
  }
]