当前位置:Gxlcms > PHP教程 > php封装的mongodb操作类

php封装的mongodb操作类

时间:2021-07-01 10:21:17 帮助过:20人阅读

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. class mongo_db {
  6. private $config;
  7. private $connection;
  8. private $db;
  9. private $connection_string;
  10. private $host;
  11. private $port;
  12. private $user;
  13. private $pass;
  14. private $dbname;
  15. private $persist;
  16. private $persist_key;
  17. private $selects = array();
  18. private $wheres = array();
  19. private $sorts = array();
  20. private $limit = 999999;
  21. private $offset = 0;
  22. private $timeout = 200;
  23. private $key = 0;
  24. /** * -------------------------------------------------------------------------------- * CONSTRUCTOR * -------------------------------------------------------------------------------- * * Automatically check if the Mongo PECL extension has been installed/enabled. * Generate the connection string and establish a connection to the MongoDB. */
  25. public function __construct() {
  26. if((IS_NOSQL != 1)){
  27. return;
  28. }
  29. if (!class_exists('Mongo')) {
  30. //$this->error("The MongoDB PECL extension has not been installed or enabled", 500);
  31. }
  32. $configs =wxcity_base::load_config("cache","mongo_db");
  33. $num = count($configs['connect']);
  34. $this->timeout = trim($configs['timeout']);
  35. $keys = wxcity_base::load_config('double');
  36. $this->key = $keys['mongo_db'];
  37. $this->config = $configs['connect'][$this->key];
  38. $status = $this->connect();
  39. if($status == false)
  40. {
  41. for($i = 1; $i < $num; $i++)
  42. {
  43. $n = $this->key + $i;
  44. $key = $n >= $num ? $n - $num : $n;
  45. $this->config = $configs['connect'][$key];
  46. $status = $this->connect();
  47. if($status!=false)
  48. {
  49. $keys['mongo_db'] = $key ;
  50. $this->key = $key;
  51. $data = "";
  52. file_put_contents(WHTY_PATH.'configs/double.php', $data, LOCK_EX);
  53. break;
  54. }
  55. }
  56. }
  57. if($status==false)
  58. {
  59. die('mongoDB not connect');
  60. }
  61. }
  62. function __destruct() {
  63. if((IS_NOSQL != 1)){
  64. return;
  65. }
  66. if($this->connection)
  67. {
  68. $this->connection->close();
  69. }
  70. }
  71. /** * -------------------------------------------------------------------------------- * CONNECT TO MONGODB * -------------------------------------------------------------------------------- * * Establish a connection to MongoDB using the connection string generated in * the connection_string() method. If 'mongo_persist_key' was set to true in the * config file, establish a persistent connection. We allow for only the 'persist' * option to be set because we want to establish a connection immediately. */
  72. private function connect() {
  73. $this->connection_string();
  74. $options = array('connect'=>true,'timeout'=>$this->timeout);
  75. try {
  76. $this->connection = new Mongo($this->connection_string, $options);
  77. $this->db = $this->connection->{$this->dbname};
  78. return($this);
  79. } catch (MongoConnectionException $e) {
  80. return false;
  81. }
  82. }
  83. /** * -------------------------------------------------------------------------------- * BUILD CONNECTION STRING * -------------------------------------------------------------------------------- * * Build the connection string from the config file. */
  84. private function connection_string() {
  85. $this->host = trim($this->config['hostname']);
  86. $this->port = trim($this->config['port']);
  87. $this->user = trim($this->config['username']);
  88. $this->pass = trim($this->config['password']);
  89. $this->dbname = trim($this->config['database']);
  90. $this->persist = trim($this->config['autoconnect']);
  91. $this->persist_key = trim($this->config['mongo_persist_key']);
  92. $connection_string = "mongodb://";
  93. if (emptyempty($this->host)) {
  94. $this->error("The Host must be set to connect to MongoDB", 500);
  95. } if (emptyempty($this->dbname)) {
  96. $this->error("The Database must be set to connect to MongoDB", 500);
  97. } if (!emptyempty($this->user) && !emptyempty($this->pass)) {
  98. $connection_string .= "{$this->user}:{$this->pass}@";
  99. } if (isset($this->port) && !emptyempty($this->port)) {
  100. $connection_string .= "{$this->host}:{$this->port}";
  101. } else {
  102. $connection_string .= "{$this->host}";
  103. } $this->connection_string = trim($connection_string);
  104. }
  105. /** * -------------------------------------------------------------------------------- * Switch_db * -------------------------------------------------------------------------------- * * Switch from default database to a different db */
  106. public function switch_db($database = '') {
  107. if (emptyempty($database)) {
  108. $this->error("To switch MongoDB databases, a new database name must be specified", 500);
  109. } $this->dbname = $database;
  110. try {
  111. $this->db = $this->connection->{$this->dbname};
  112. return(TRUE);
  113. } catch (Exception $e) {
  114. $this->error("Unable to switch Mongo Databases: {$e->getMessage()}", 500);
  115. }
  116. }
  117. /** * -------------------------------------------------------------------------------- * SELECT FIELDS * -------------------------------------------------------------------------------- * * Determine which fields to include OR which to exclude during the query process. * Currently, including and excluding at the same time is not available, so the * $includes array will take precedence over the $excludes array. If you want to * only choose fields to exclude, leave $includes an empty array(). * * @usage: $this->mongo_db->select(array('foo', 'bar'))->get('foobar'); */
  118. public function select($includes = array(), $excludes = array()) {
  119. if (!is_array($includes)) {
  120. $includes = array();
  121. }
  122. if (!is_array($excludes)) {
  123. $excludes = array();
  124. }
  125. if (!emptyempty($includes)) {
  126. foreach ($includes as $col) {
  127. $this->selects[$col] = 1;
  128. }
  129. } else {
  130. foreach ($excludes as $col) {
  131. $this->selects[$col] = 0;
  132. }
  133. } return($this);
  134. }
  135. /** * -------------------------------------------------------------------------------- * WHERE PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents based on these search parameters. The $wheres array should * be an associative array with the field as the key and the value as the search * criteria. * * @usage = $this->mongo_db->where(array('foo' => 'bar'))->get('foobar'); */
  136. public function where($wheres = array()) {
  137. foreach ((array)$wheres as $wh => $val) {
  138. $this->wheres[$wh] = $val;
  139. } return($this);
  140. }
  141. /** * -------------------------------------------------------------------------------- * WHERE_IN PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is in a given $in array(). * * @usage = $this->mongo_db->where_in('foo', array('bar', 'zoo', 'blah'))->get('foobar'); */
  142. public function where_in($field = "", $in = array()) {
  143. $this->where_init($field);
  144. $this->wheres[$field]['$in'] = $in;
  145. return($this);
  146. }
  147. /** * -------------------------------------------------------------------------------- * WHERE_NOT_IN PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is not in a given $in array(). * * @usage = $this->mongo_db->where_not_in('foo', array('bar', 'zoo', 'blah'))->get('foobar'); */
  148. public function where_not_in($field = "", $in = array()) {
  149. $this->where_init($field);
  150. $this->wheres[$field]['$nin'] = $in;
  151. return($this);
  152. }
  153. /** * -------------------------------------------------------------------------------- * WHERE GREATER THAN PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is greater than $x * * @usage = $this->mongo_db->where_gt('foo', 20); */
  154. public function where_gt($field = "", $x) {
  155. $this->where_init($field);
  156. $this->wheres[$field]['$gt'] = $x;
  157. return($this);
  158. }
  159. /** * -------------------------------------------------------------------------------- * WHERE GREATER THAN OR EQUAL TO PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is greater than or equal to $x * * @usage = $this->mongo_db->where_gte('foo', 20); */
  160. public function where_gte($field = "", $x) {
  161. $this->where_init($field);
  162. $this->wheres[$field]['$gte'] = $x;
  163. return($this);
  164. }
  165. /** * -------------------------------------------------------------------------------- * WHERE LESS THAN PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is less than $x * * @usage = $this->mongo_db->where_lt('foo', 20); */
  166. public function where_lt($field = "", $x) {
  167. $this->where_init($field);
  168. $this->wheres[$field]['$lt'] = $x;
  169. return($this);
  170. }
  171. /** * -------------------------------------------------------------------------------- * WHERE LESS THAN OR EQUAL TO PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is less than or equal to $x * * @usage = $this->mongo_db->where_lte('foo', 20); */
  172. public function where_lte($field = "", $x) {
  173. $this->where_init($field);
  174. $this->wheres[$field]['$lte'] = $x;
  175. return($this);
  176. }
  177. /** * -------------------------------------------------------------------------------- * WHERE BETWEEN PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is between $x and $y * * @usage = $this->mongo_db->where_between('foo', 20, 30); */
  178. public function where_between($field = "", $x, $y) {
  179. $this->where_init($field);
  180. $this->wheres[$field]['$gte'] = $x;
  181. $this->wheres[$field]['$lte'] = $y;
  182. return($this);
  183. }
  184. /** * -------------------------------------------------------------------------------- * WHERE BETWEEN AND NOT EQUAL TO PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is between but not equal to $x and $y * * @usage = $this->mongo_db->where_between_ne('foo', 20, 30); */
  185. public function where_between_ne($field = "", $x, $y) {
  186. $this->where_init($field);
  187. $this->wheres[$field]['$gt'] = $x;
  188. $this->wheres[$field]['$lt'] = $y;
  189. return($this);
  190. }
  191. /** * -------------------------------------------------------------------------------- * WHERE NOT EQUAL TO PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is not equal to $x * * @usage = $this->mongo_db->where_between('foo', 20, 30); */
  192. public function where_ne($field = "", $x) {
  193. $this->where_init($field);
  194. $this->wheres[$field]['$ne'] = $x;
  195. return($this);
  196. }
  197. /** * -------------------------------------------------------------------------------- * WHERE OR * -------------------------------------------------------------------------------- * * Get the documents where the value of a $field is in one or more values * * @usage = $this->mongo_db->where_or('foo', array( 'foo', 'bar', 'blegh' ); */
  198. public function where_or($field = "", $values) {
  199. $this->where_init($field);
  200. $this->wheres[$field]['$or'] = $values;
  201. return($this);
  202. }
  203. /** * -------------------------------------------------------------------------------- * WHERE AND * -------------------------------------------------------------------------------- * * Get the documents where the elements match the specified values * * @usage = $this->mongo_db->where_and( array ( 'foo' => 1, 'b' => 'someexample' ); */
  204. public function where_and($elements_values = array()) {
  205. foreach ((array)$elements_values as $element => $val) {
  206. $this->wheres[$element] = $val;
  207. } return($this);
  208. }
  209. /** * -------------------------------------------------------------------------------- * WHERE MOD * -------------------------------------------------------------------------------- * * Get the documents where $field % $mod = $result * * @usage = $this->mongo_db->where_mod( 'foo', 10, 1 ); */
  210. public function where_mod($field, $num, $result) {
  211. $this->where_init($field);
  212. $this->wheres[$field]['$mod'] = array($num, $result);
  213. return($this);
  214. }
  215. /** * -------------------------------------------------------------------------------- * Where size * -------------------------------------------------------------------------------- * * Get the documents where the size of a field is in a given $size int * * @usage : $this->mongo_db->where_size('foo', 1)->get('foobar'); */
  216. public function where_size($field = "", $size = "") {
  217. $this->_where_init($field);
  218. $this->wheres[$field]['$size'] = $size;
  219. return ($this);
  220. }
  221. /** * -------------------------------------------------------------------------------- * LIKE PARAMETERS * -------------------------------------------------------------------------------- * * Get the documents where the (string) value of a $field is like a value. The defaults * allow for a case-insensitive search. * * @param $flags * Allows for the typical regular expression flags: * i = case insensitive * m = multiline * x = can contain comments * l = locale * s = dotall, "." matches everything, including newlines * u = match unicode * * @param $enable_start_wildcard * If set to anything other than TRUE, a starting line character "^" will be prepended * to the search value, representing only searching for a value at the start of * a new line. * * @param $enable_end_wildcard * If set to anything other than TRUE, an ending line character "$" will be appended * to the search value, representing only searching for a value at the end of * a line. * * @usage = $this->mongo_db->like('foo', 'bar', 'im', FALSE, TRUE); */
  222. public function like($field = "", $value = "", $flags = "i", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE) {
  223. $field = (string) trim($field);
  224. $this->where_init($field);
  225. $value = (string) trim($value);
  226. $value = quotemeta($value);
  227. if ($enable_start_wildcard !== TRUE) {
  228. $value = "^" . $value;
  229. } if ($enable_end_wildcard !== TRUE) {
  230. $value .= "$";
  231. } $regex = "/$value/$flags";
  232. $this->wheres[$field] = new MongoRegex($regex);
  233. return($this);
  234. }
  235. public function wheres($where){
  236. $this->wheres = $where;
  237. }
  238. /** * -------------------------------------------------------------------------------- * ORDER BY PARAMETERS * -------------------------------------------------------------------------------- * * Sort the documents based on the parameters passed. To set values to descending order, * you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be * set to 1 (ASC). * * @usage = $this->mongo_db->where_between('foo', 20, 30); */
  239. public function order_by($fields = array()) {
  240. if (!is_array($fields) || !count($fields)) return ;
  241. foreach ($fields as $col => $val) {
  242. if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
  243. $this->sorts[$col] = -1;
  244. } else {
  245. $this->sorts[$col] = 1;
  246. }
  247. } return($this);
  248. }
  249. /** * -------------------------------------------------------------------------------- * LIMIT DOCUMENTS * -------------------------------------------------------------------------------- * * Limit the result set to $x number of documents * * @usage = $this->mongo_db->limit($x); */
  250. public function limit($x = 99999) {
  251. if ($x !== NULL && is_numeric($x) && $x >= 1) {
  252. $this->limit = (int) $x;
  253. } return($this);
  254. }
  255. /** * -------------------------------------------------------------------------------- * OFFSET DOCUMENTS * -------------------------------------------------------------------------------- * * Offset the result set to skip $x number of documents * * @usage = $this->mongo_db->offset($x); */
  256. public function offset($x = 0) {
  257. if ($x !== NULL && is_numeric($x) && $x >= 1) {
  258. $this->offset = (int) $x;
  259. } return($this);
  260. }
  261. /** * -------------------------------------------------------------------------------- * GET_WHERE * -------------------------------------------------------------------------------- * * Get the documents based upon the passed parameters * * @usage = $this->mongo_db->get_where('foo', array('bar' => 'something')); */
  262. public function get_where($collection = "", $where = array(), $limit = 99999, $orderby=array()) {
  263. if (is_array($orderby) || !emptyempty($orderby)) {
  264. $order_by = $this->order_by($order_by);
  265. }
  266. return($this->where($where)->limit($limit)->get($collection));
  267. }
  268. public function selectA($collection = "", $limit = 99999, $orderby=array()) {
  269. if(intval($limit)<1){
  270. $limit = 999999;
  271. }
  272. $order_by = $this->order_by($orderby);
  273. $re = $this->limit($limit)->get($collection);
  274. $this->clear();
  275. return (array)$re;
  276. }
  277. public function listinfo($collection = "", $orderby=array(), $page=1, $pagesize=12) {
  278. $page = max(intval($page), 1);
  279. $offset = $pagesize * ($page - 1);
  280. $pagesizes = $offset + $pagesize;
  281. $this->offset($offset);
  282. $order_by = $this->order_by($orderby);
  283. $re = $this->limit($pagesize)->get($collection);
  284. $this->limit(999999);
  285. $count = $this->count($collection);
  286. $this->pages = pages($count, $page, $pagesize);
  287. return (array)$re;
  288. }
  289. /** * -------------------------------------------------------------------------------- * GET * -------------------------------------------------------------------------------- * * Get the documents based upon the passed parameters * * @usage = $this->mongo_db->get('foo', array('bar' => 'something')); */
  290. public function get($collection = "") {
  291. if (emptyempty($collection)) {
  292. $this->error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
  293. } $results = array();
  294. $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit)->skip((int) $this->offset)->sort($this->sorts);
  295. $returns = array();
  296. foreach ($documents as $doc): $returns[] = $doc;
  297. endforeach;
  298. return($returns);
  299. }
  300. public function getMy($collection = "") {
  301. if (emptyempty($collection)) {
  302. $this->error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
  303. } $results = array();
  304. $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit)->skip((int) $this->offset)->sort($this->sorts);
  305. $returns = array();
  306. foreach ($documents as $doc): $returns[] = $doc;
  307. endforeach;
  308. $this -> clear();
  309. return($returns);
  310. }
  311. /** * -------------------------------------------------------------------------------- * COUNT * -------------------------------------------------------------------------------- * * Count the documents based upon the passed parameters * * @usage = $this->mongo_db->get('foo'); */
  312. public function count($collection = "") {
  313. if (emptyempty($collection)) {
  314. $this->error("In order to retreive a count of documents from MongoDB, a collection name must be passed", 500);
  315. } $count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)->skip((int) $this->offset)->count();
  316. $this->clear();
  317. return($count);
  318. }
  319. /** * -------------------------------------------------------------------------------- * INSERT * -------------------------------------------------------------------------------- * * Insert a new document into the passed collection * * @usage = $this->mongo_db->insert('foo', $data = array()); */
  320. public function insert($collection = "", $data = array(), $name='ID') {
  321. if (emptyempty($collection)) {
  322. $this->error("No Mongo collection selected to insert into", 500);
  323. } if (count($data) == 0 || !is_array($data)) {
  324. $this->error("Nothing to insert into Mongo collection or insert is not an array", 500);
  325. } try {
  326. /**
  327. wxcity_base::load_sys_class('whtysqs','',0);
  328. $mongoseq_class = new whtysqs('creaseidsqs');
  329. $re = $mongoseq_class->query("?name=" . $collection . "&opt=put&data=1");
  330. **/
  331. $re = put_sqs('list_mongo_creaseidsqs','1');
  332. if(is_numeric($re)){
  333. $re++;
  334. $data[$name] = intval($re);
  335. }else{
  336. $data[$name] = intval(time());
  337. //die('mongosqs error');
  338. }
  339. $this->db->{$collection}->insert($data, array('fsync' => TRUE));
  340. $this->clear();
  341. return $data[$name];
  342. } catch (MongoCursorException $e) {
  343. $this->error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
  344. }
  345. }
  346. public function insertWithId($collection = "", $data = array()) {
  347. if (emptyempty($collection)) {
  348. $this->error("No Mongo collection selected to insert into", 500);
  349. } if (count($data) == 0 || !is_array($data)) {
  350. $this->error("Nothing to insert into Mongo collection or insert is not an array", 500);
  351. } try {
  352. $this->db->{$collection}->insert($data, array('fsync' => TRUE));
  353. $this->clear();
  354. return 1;
  355. } catch (MongoCursorException $e) {
  356. $this->error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
  357. }
  358. }
  359. /** * -------------------------------------------------------------------------------- * UPDATE * -------------------------------------------------------------------------------- * * Update a document into the passed collection * * @usage = $this->mongo_db->update('foo', $data = array()); */
  360. public function update($collection = "", $data = array()) {
  361. if (emptyempty($collection)) {
  362. $this->error("No Mongo collection selected to update", 500);
  363. } if (count($data) == 0 || !is_array($data)) {
  364. $this->error("Nothing to update in Mongo collection or update is not an array", 500);
  365. } try {
  366. $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => TRUE, 'multiple' => FALSE));
  367. $this->clear();
  368. return(TRUE);
  369. } catch (MongoCursorException $e) {
  370. $this->error("Update of data into MongoDB failed: {$e->getMessage()}", 500);
  371. }
  372. }
  373. /** * -------------------------------------------------------------------------------- * UPDATE_ALL * -------------------------------------------------------------------------------- * * Insert a new document into the passed collection * * @usage = $this->mongo_db->update_all('foo', $data = array()); */
  374. public function update_all($collection = "", $data = array()) {
  375. if (emptyempty($collection)) {
  376. $this->error("No Mongo collection selected to update", 500);
  377. } if (count($data) == 0 || !is_array($data)) {
  378. $this->error("Nothing to update in Mongo collection or update is not an array", 500);
  379. } try {
  380. $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => TRUE, 'multiple' => TRUE));
  381. return(TRUE);
  382. } catch (MongoCursorException $e) {
  383. $this->error("Update of data into MongoDB failed: {$e->getMessage()}", 500);
  384. }
  385. }
  386. /** * -------------------------------------------------------------------------------- * DELETE * -------------------------------------------------------------------------------- * * delete document from the passed collection based upon certain criteria * * @usage = $this->mongo_db->delete('foo', $data = array()); */
  387. public function delete($collection = "") {
  388. if (emptyempty($collection)) {
  389. $this->error("No Mongo collection selected to delete from", 500);
  390. } try {
  391. $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => TRUE));
  392. $this->clear();
  393. return(TRUE);
  394. } catch (MongoCursorException $e) {
  395. $this->error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
  396. }
  397. }
  398. /** * -------------------------------------------------------------------------------- * DELETE_ALL * -------------------------------------------------------------------------------- * * Delete all documents from the passed collection based upon certain criteria * * @usage = $this->mongo_db->delete_all('foo', $data = array()); */
  399. public function delete_all($collection = "") {
  400. if (emptyempty($collection)) {
  401. $this->error("No Mongo collection selected to delete from", 500);
  402. } try {
  403. $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => FALSE));
  404. return(TRUE);
  405. } catch (MongoCursorException $e) {
  406. $this->error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
  407. }
  408. }
  409. /** * -------------------------------------------------------------------------------- * ADD_INDEX * -------------------------------------------------------------------------------- * * Ensure an index of the keys in a collection with optional parameters. To set values to descending order, * you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be * set to 1 (ASC). * * @usage = $this->mongo_db->add_index($collection, array('first_name' => 'ASC', 'last_name' => -1), array('unique' => TRUE)); */
  410. public function add_index($collection = "", $keys = array(), $options = array()) {
  411. if (emptyempty($collection)) {
  412. $this->error("No Mongo collection specified to add index to", 500);
  413. } if (emptyempty($keys) || !is_array($keys)) {
  414. $this->error("Index could not be created to MongoDB Collection because no keys were specified", 500);
  415. } foreach ($keys as $col => $val) {
  416. if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
  417. $keys[$col] = -1;
  418. } else {
  419. $keys[$col] = 1;
  420. }
  421. } if ($this->db->{$collection}->ensureIndex($keys, $options) == TRUE) {
  422. $this->clear();
  423. return($this);
  424. } else {
  425. $this->error("An error occured when trying to add an index to MongoDB Collection", 500);
  426. }
  427. }
  428. /** * -------------------------------------------------------------------------------- * REMOVE_INDEX * -------------------------------------------------------------------------------- * * Remove an index of the keys in a collection. To set values to descending order, * you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be * set to 1 (ASC). * * @usage = $this->mongo_db->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1)); */
  429. public function remove_index($collection = "", $keys = array()) {
  430. if (emptyempty($collection)) {
  431. $this->error("No Mongo collection specified to remove index from", 500);
  432. } if (emptyempty($keys) || !is_array($keys)) {
  433. $this->error("Index could not be removed from MongoDB Collection because no keys were specified", 500);
  434. } if ($this->db->{$collection}->deleteIndex($keys, $options) == TRUE) {
  435. $this->clear();
  436. return($this);
  437. } else {
  438. $this->error("An error occured when trying to remove an index from MongoDB Collection", 500);
  439. }
  440. }
  441. /** * -------------------------------------------------------------------------------- * REMOVE_ALL_INDEXES * -------------------------------------------------------------------------------- * * Remove all indexes from a collection. * * @usage = $this->mongo_db->remove_all_index($collection); */
  442. public function remove_all_indexes($collection = "") {
  443. if (emptyempty($collection)) {
  444. $this->error("No Mongo collection specified to remove all indexes from", 500);
  445. } $this->db->{$collection}->deleteIndexes();
  446. $this->clear();
  447. return($this);
  448. }
  449. /** * -------------------------------------------------------------------------------- * LIST_INDEXES * -------------------------------------------------------------------------------- * * Lists all indexes in a collection. * * @usage = $this->mongo_db->list_indexes($collection); */
  450. public function list_indexes($collection = "") {
  451. if (emptyempty($collection)) {
  452. $this->error("No Mongo collection specified to remove all indexes from", 500);
  453. } return($this->db->{$collection}->getIndexInfo());
  454. }
  455. /** * -------------------------------------------------------------------------------- * DROP COLLECTION * -------------------------------------------------------------------------------- * * Removes the specified collection from the database. Be careful because this * can have some very large issues in production! */
  456. public function drop_collection($collection = "") {
  457. if (emptyempty($collection)) {
  458. $this->error("No Mongo collection specified to drop from database", 500);
  459. } $this->db->{$collection}->drop();
  460. return TRUE;
  461. }
  462. /** * -------------------------------------------------------------------------------- * CLEAR * -------------------------------------------------------------------------------- * * Resets the class variables to default settings */
  463. private function clear() {
  464. $this->selects = array();
  465. $this->wheres = array();
  466. $this->limit = NULL;
  467. $this->offset = NULL;
  468. $this->sorts = array();
  469. }
  470. /** * -------------------------------------------------------------------------------- * WHERE INITIALIZER * -------------------------------------------------------------------------------- * * Prepares parameters for insertion in $wheres array(). */
  471. private function where_init($param) {
  472. if (!isset($this->wheres[$param])) {
  473. $this->wheres[$param] = array();
  474. }
  475. }
  476. public function error($str, $t) {
  477. echo $str;
  478. exit;
  479. }
  480. }
  481. ?>

使用范例
  1. $table_name=trim(strtolower($this->table_name));
  2. $this->mongo_db->where($where);
  3. $order=!emptyempty($order)?array('AID'=>'DESC'):array('AID'=>'ASC');//升序降序
  4. $infos=$this->mongo_db->listinfo($table_name,$order,$page,$pagesize);

php, mongodb

人气教程排行