国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 後端開發(fā) php教程 php封裝的mongodb操作類

php封裝的mongodb操作類

Jul 25, 2016 am 08:43 AM

  1. /*
  2. * ? ???? ????? ?? | ???
  3. * ????? ???? ???.
  4. */
  5. class mongo_db {
  6. private $config;
  7. ??? $connection;
  8. ??? $db;
  9. ??? $connection_string;
  10. ??? $host;
  11. ??? $port;
  12. ?? $user;
  13. ??? $pass;
  14. ??? $dbname;
  15. ??? $persist;
  16. ??? $persist_key;
  17. ??? $selects = array();
  18. ??? $wheres = array();
  19. ??? $sorts = array();
  20. ??? $limit = 999999;
  21. ??? $offset = 0;
  22. ??? $timeout = 200;
  23. ?? $key = 0;
  24. /*** ------------------------------------------------- ------------------ * ??? * --- ------------------------------------- -------------- * * Mongo PECL ??? ??/??????? ???? ?????. * ?? ???? ???? MongoDB? ?? ??? ?????.*/
  25. ?? ?? __construct() {
  26. if((IS_NOSQL != 1)){
  27. return;
  28. }
  29. if (!class_exists('Mongo')) {
  30. //$this->error("MongoDB PECL ??? ???? ???? ????? ?????.", 500);
  31. }
  32. $configs =wxcity_base::load_config("??","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-> ? $i;
  44. $key = $n >= $num ? $n - $?? : $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. ??;
  54. }
  55. }
  56. }
  57. if($status==false)
  58. {
  59. die('mongoDB? ???? ??');
  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. /*** ------------------------------------------------- ------------------ * MONGODB? ?? * -------------- ------------------------------------- ---------------- * * * Connection_string() ????? ??? ?? ???? ???? MongoDB? ?? ??? ?????. * ?? ???? 'mongo_persist_key'? true? ??? ?? ?? ??? ?????. ?? ??? ????? ?? ??? 'persist' * ??? ????? ?????.*/
  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. /*** ------------------------------------------------- ------------------ * ?? ??? ?? * -------------- ------------------------------------- ---------------- * * ?? ???? ?? ???? ?????.*/
  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("MongoDB? ????? ???? ???? ???.", 500);
  95. } if (emptyempty($this->dbname)) {
  96. $this->error("MongoDB? ????? ??????? ???? ???.", 500);
  97. } if (!emptyempty($this->user) && !emptyempty($this->pass)) {
  98. $connection_string .= "{$this->user}:{$this- >??}@";
  99. } if (isset($this->port) && !emptyempty($this->port)) {
  100. $connection_string .= "{$this->host}:{$this-> ;??}";
  101. } else {
  102. $connection_string .= "{$this->host}";
  103. } $this->connection_string = ??($connection_string);
  104. }
  105. /*** ------------------------------------------------- ------------------ * Switch_db * --- ------------------------------------- -------------- * * ?? ???????? ?? ??????? ??*/
  106. public function switch_db($database = '') {
  107. if (emptyempty($database)) {
  108. $this- >error("MongoDB ??????? ????? ? ?????? ??? ???? ???.", 500);
  109. } $this->dbname = $database;
  110. try {
  111. $this->db = $this->connection->{$this->dbname};
  112. ??(TRUE);
  113. } catch (Exception $e) {
  114. $this->error("Mongo ??????? ??? ? ????: {$e->getMessage()}", 500);
  115. }
  116. }
  117. /*** ------------------------------------------------- ------------------ * ?? ?? * --------------- ------------------------------------- --------------- * * ?? ???? ?? ??? ?? ?? ??? ??? ?????. * ??? ??? ??? ??? ??? ? ???? * $includes ??? $excludes ???? ?????. * ??? ??? ????? $includes? ? ??()? ?? ????. * * @usage: $this->mongo_db->select(array('foo', 'bar'))->get('foobar');*/
  118. ?? ?? select($includes = array(), $excludes = array()) {
  119. if (! is_array($includes)) {
  120. $includes = ??();
  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. /*** ------------------------------------------------- ------------------ * ????? ?? * --------------- ------------------------------------- --------------- * * ??? ?? ????? ???? ??? ?????. $wheres ??? ??? ??, ?? ?? ???? * ?? ????? ???. * * @usage = $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');*/
  136. ?? ?? where($wheres = array()) {
  137. foreach ((array)$wheres as $wh => $ val) {
  138. $this->wheres[$wh] = $val;
  139. } return($this);
  140. }
  141. /*** ------------------------------------------------- ------------------------------- * WHERE_IN 參數(shù) * --------------- -------------------------------------------------- --------------- * * 取得給定$in array() 中$field 的值所在的文件。 * * @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. 返回($this);
  146. }
  147. /*** ------------------------------------------------- ------------------------------- * WHERE_NOT_IN 參數(shù) * --------------- --- ----------------------------------------------- --- --------------- * * 取得$field 的值不在給定$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($現(xiàn)場);
  150. $this->wheres[$field]['$nin'] = $in;
  151. 回傳($this); }
  152. /*** ------------------------------------------------- ------------------------------- * 大於參數(shù) * ------------- -- ------------------------------------------------ -- --------------- * * 取得$field的值大於$x的文檔* * @usage = $this->mongo_db->where_gt( '富', 20);* /
  153. public function where_gt($field = "", $x) {
  154. $this->where_init($field);
  155. $this->wheres[$field]['$gt'] = $x;
  156. 回傳($this)
  157. }
  158. /*** ------------------------------------------------- ------------------------------- * 大於或等於參數(shù) * ---------- --- ----------------------------------------------- --- ----------------- * * 取得$field的值大於等於$x的文件* * @usage = $this-> mongo_db->where_gte('foo', 20) ;*/
  159. public function where_gte($field = "", $x) {
  160. $this-> where_init($field);
  161. $this->wheres[$field]['$gte'] = $x;
  162. 回傳($this)
  163. }
  164. /*** ------------------------------------------------- ------------------------------- * 小於參數(shù)的地方 * ------------- -------------------------------------------------- ----------------- * * 取得$field的值小於$x的文檔* * @usage = $this->mongo_db->where_lt( '富', 20);*/
  165. public function where_lt($field = "", $x) {
  166. $this->where_init($field);
  167. $this->wheres[$field ]['$lt '] = $x;
  168. 回傳($this) }
  169. /*** ------------------------------------------------- ------------------------------- * 小於或等於參數(shù) * ---------- --- ----------------------------------------------- --- ----------------- * * 取得$field的值小於或等於$x的文檔* * @usage = $this-> mongo_db->where_lte('foo', 20 );*/
  170. public function where_lte($field = "" , $x) {
  171. $ this->where_init($field)
  172. $this->wheres[$field]['$lte'] = $x;
  173. 回傳($this); ??> }
  174. /* ** ------------------------------------------------- ------------------------------- * 參數(shù)之間的位置* ------------ -- ------------------------------------------------ -- ---------------- * * 取得$field的值在$x和$y之間的文件* * @usage = $this->mongo_db->where_ Between ( 'foo', 20, 30);*/
  175. public function where_ Between($field = "", $x, $y) {
  176. $this->where_init($field ) ;
  177. $this->wheres[$ field]['$gte'] = $x;
  178. $this->wheres[$field]['$lte'] = $y;
  179. 回傳( $這個);
  180. }
  181. /*** ------------------------------------------------- ------------------ * ???? ?? ? ????? ?? ?? * ---------- ------------------------------------- ------- * * $field? ?? $x? $y ????? ?? ?? ??? ?????. * * @usage = $this ->mongo_db->where_between_ne('foo', 20, 30);*/
  182. ?? ?? where_between_ne($field = "", $x, $y) {
  183. $this->where_init($field);
  184. $this->wheres[$field]['$gt'] = $x;
  185. $this->wheres[$field]['$lt'] = $y;
  186. return($this);
  187. }
  188. /*** ------------------------------------------------- ------------------ * ????? ?? ?? ?? * ------------ ------------------------------------- ------------------ * * $field? ?? $x? ?? ?? ??? ?????. * * @usage = $this->mongo_db-> where_between('foo', 20, 30);*/
  189. ?? ?? where_ne($field = "", $x) {
  190. $this->where_init($field);
  191. $this->wheres[$field]['$ne'] = $x;
  192. return($this);
  193. }
  194. /*** ------------------------------------------------- ------------------ * ??? ?? * --------------- ------------------------------------- --------------- * * $field? ?? ?? ??? ?? ?? ?? ???? * * @usage = $this->mongo_db->where_or(' foo', array( 'foo', 'bar', 'blegh' );*/
  195. ?? ?? where_or($field = "", $values) {
  196. $this->where_init($field);
  197. $this->wheres[$field]['$or'] = $values;
  198. return($this);
  199. }
  200. /*** ------------------------------------------------- ------------------ * ??? ??? * --------------- ------------------------------------- --------------- * * ??? ??? ?? ???? ?? ???? * * @usage = $this->mongo_db->where_and( array ( 'foo' => ; 1, 'b' => '?? ?' );*/
  201. ?? ?? where_and($elements_values ??= array()) {
  202. foreach ((array)$elements_values ??as $element => $ val) {
  203. $this->wheres[$element] = $val;
  204. } return($this);
  205. }
  206. /*** ------------------------------------------------- ------------------ * MOD? ??? * --------------- ------------------------------------- --------------- * * $field % $mod = $result * * @usage = $this->mongo_db->where_mod( 'foo', 10 ?? ?? ???? , 1 );*/
  207. ?? ?? where_mod($field, $num, $result) {
  208. $this->where_init($field);
  209. $this->wheres[$field]['$mod'] = array($num, $result);
  210. return($this);
  211. }
  212. /*** ------------------------------------------------- ------------------ * ???? * --------------- ------------------------------------- --------------- * * ?? ??? ??? $size int? ?? ???? * * @usage : $this->mongo_db->where_size(' foo', 1)->get('foobar');*/
  213. ?? ?? where_size($field = "", $size = "") {
  214. $this->_where_init($field );
  215. $this->wheres[$field]['$size'] = $size;
  216. ??($this);
  217. }
  218. /*** ------------------------------------------------- ------------------ * ????? ??? * --------------- ------------------------------------- --------------- * * $field? (???) ?? ?? ??? ??? ?????. ???? * ????? ???? ?? ??? ?????. * * @param $flags * ???? ??? ???? ?????. * i = ????? ???? ?? * m = ?? ? * x = ?? ?? ?? * l = ??? * s = dotall, "." ? ??? ???? ?? ??? ?? * u = ???? ?? * * @param $enable_start_wildcard * TRUE ??? ??? ???? ?? ? ?? "^"? ?? ? ?? * ?????. ?? ?? ? ?? ????? ?????. * ??? ?. * * @param $enable_end_wildcard * TRUE ??? ??? ???? ? ? ?? "$"? ?? ?? ?????. ?? ? ?? ?? ????? ?? ?????. * * @usage = $this->mongo_db->like('foo', 'bar', 'im', FALSE, TRUE);*/
  219. ?? ?? like($field = "", $value = "", $flags = "i", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE) {
  220. $field = (???) Trim($field);
  221. $this->where_init($field);
  222. $value = (???) Trim($value);
  223. $value = quotemeta($value);
  224. if ($enable_start_wildcard !== TRUE) {
  225. $value = "^" . $?;
  226. } if ($enable_end_wildcard !== TRUE) {
  227. $value .= "$";
  228. } $regex = "/$value/$flags";
  229. $this->wheres[$field] = new MongoRegex($regex);
  230. return($this);
  231. }
  232. ?? ?? wheres($where){
  233. $this->wheres = $where;
  234. }
  235. /*** ------------------------------------------------- ------------------ * ????? ?? * -------------- ------------------------------------- ---------------- * * ??? ????? ???? ??? ?????. ?? ?????? ????? * -1, FALSE, 'desc' ?? 'DESC' ?? ???? ???. ??? ??? * 1(ASC)? ?????. * * @usage = $this->mongo_db->where_between('foo', 20, 30);*/
  236. ?? ?? order_by($fields = array()) {
  237. if (!is_array($fields) || !count($ ??)) return ;
  238. foreach ($fields as $col => $val) {
  239. if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
  240. $this->sorts[$col] = -1;
  241. } else {
  242. $this->sorts[$col] = 1;
  243. }
  244. } return($this);
  245. }
  246. /*** ------------------------------------------------- ------------------ * ??? ?? * --------------- ------------------------------------- --------------- * * ?? ??? ?? ? $x? ?? * * @usage = $this->mongo_db->limit($x);*/
  247. ?? ?? ??($x = 99999) {
  248. if ($x !== NULL && is_numeric($x) && $ x >= 1) {
  249. $this->limit = (int) $x;
  250. } return($this);
  251. }
  252. /*** ------------------------------------------------- ------------------ * ??? ?? * --------------- ------------------------------------- --------------- * * $x?? ??? ????? ?? ??? ??????. * * @usage = $this->mongo_db->offset($x);*/
  253. ?? ?? ???($x = 0) {
  254. if ($x !== NULL && is_numeric($x) && $ x >= 1) {
  255. $this->offset = (int) $x;
  256. } return($this);
  257. }
  258. /*** ------------------------------------------------- ------------------ * GET_WHERE * --- ------------------------------------- -------------- * * ??? ????? ???? ?? ???? * * @usage = $this->mongo_db->get_where('foo', array('bar' = > '??'));*/
  259. ?? ?? get_where($collection = "", $where = array(), $limit = 99999, $orderby=array()) {
  260. if (is_array($orderby) || !emptyempty($orderby)) {
  261. $order_by = $this->order_by($order_by);
  262. }
  263. return($this->where($where)->limit($limit)->get($collection));
  264. }
  265. ?? ?? selectA($collection = "", $limit = 99999, $orderby=array()) {
  266. if(intval($limit)<1){
  267. $limit = 999999;
  268. }
  269. $order_by = $this->order_by($orderby);
  270. $re = $this->limit($limit)->get($collection);
  271. $this->clear();
  272. return (??)$re;
  273. }
  274. ?? ?? listinfo($collection = "", $orderby=array(), $page=1, $pagesize=12) {
  275. $page = max(intval($page) ), 1);
  276. $offset = $pagesize * ($page - 1);
  277. $pagesizes = $offset $pagesize;
  278. $this->offset($offset);
  279. $order_by = $this->order_by($orderby);
  280. $re = $this->limit($pagesize)->get($collection);
  281. $this->limit(999999);
  282. $count = $this->count($collection);
  283. $this->pages = ???($count, $page, $pagesize);
  284. return (??)$re;
  285. }
  286. /*** ------------------------------------------------- ------------------------------- * ?? * ---------------- ------------------------------------- -------------- * * ??? ????? ???? ?? ???? * * @usage = $this->mongo_db->get('foo', array('bar' = > '??'));*/
  287. ?? ?? get($collection = "") {
  288. if (emptyempty($collection)) {
  289. $this- >error("MongoDB?? ??? ????? ??? ??? ???? ???.", 500);
  290. } $?? = ??();
  291. $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this-> ;limit)->skip((int) $this->offset)->sort($this->sorts);
  292. $returns = ??();
  293. foreach($documents? $doc?): $returns[] = $doc;
  294. endforeach;
  295. ??($returns);
  296. }
  297. public function getMy($collection = "") {
  298. if (emptyempty($collection)) {
  299. $this->error("???? ??? ????? MongoDB, ??? ??? ???? ???.", 500);
  300. } $?? = ??();
  301. $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this-> ;limit)->skip((int) $this->offset)->sort($this->sorts);
  302. $returns = ??();
  303. foreach($documents? $doc?): $returns[] = $doc;
  304. endforeach;
  305. $?? -> ???();
  306. ??($returns);
  307. }
  308. /*** ------------------------------------------------- ------------------------------- * ?? * ---------------- ------------------------------------- -------------- * * ??? ????? ???? ?? ?? * * @usage = $this->mongo_db->get('foo');*/
  309. ?? ?? ??($collection = "") {
  310. if (emptyempty($collection)) {
  311. $this- >error("MongoDB?? ?? ??? ????? ??? ??? ???? ???.", 500);
  312. } $count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)-> Skip((int) $this->offset)->count();
  313. $this->clear();
  314. ??($count);
  315. }
  316. /*** ------------------------------------------------- ------------------ * ?? * --- ------------------------------------- -------------- * * ??? ???? ? ?? ?? * * @usage = $this->mongo_db->insert('foo', $data = array() );*/
  317. ?? ?? insert($collection = "", $data = array(), $name='ID') {
  318. if (emptyempty($collection)) {
  319. $this->error("??? Mongo ???? ???? ?????.", 500);
  320. } if (count($data) == 0 || !is_array($data)) {
  321. $this->error("Mongo ???? ??? ??? ??? ??? ??? ??? ????.", 500 );
  322. } try {
  323. /**
  324. wxcity_base::load_sys_class('whtysqs','',0);
  325. $mongoseq_class = new whtysqs('creaseidsqs');
  326. $re = $mongoseq_class->query("?name=" . $collection . "&opt=put&data=1");
  327. **/
  328. $re = put_sqs('list_mongo_creaseidsqs','1');
  329. if(is_numeric($re)){
  330. $re ;
  331. $data[$name] = intval($re);
  332. }else{
  333. $data[$name] = intval(time());
  334. //die('mongosqs ??');
  335. }
  336. $this->db->{$collection}->insert($data, array('fsync' => TRUE));
  337. $this->clear();
  338. $data[$name] ??;
  339. } catch (MongoCursorException $e) {
  340. $this->error("MongoDB? ??? ?? ??: {$e->getMessage()}", 500);
  341. }
  342. }
  343. ?? ?? insertWithId($collection = "", $data = array()) {
  344. if (emptyempty($collection)) {
  345. $this- >error("??? Mongo ???? ???? ?????.", 500);
  346. } if (count($data) == 0 || !is_array($data)) {
  347. $this->error("Mongo ???? ??? ??? ??? ??? ??? ??? ????.", 500 );
  348. } try {
  349. $this->db->{$collection}->insert($data, array('fsync' => TRUE));
  350. $this->clear();
  351. 1? ?????.
  352. } catch (MongoCursorException $e) {
  353. $this->error("MongoDB? ??? ?? ??: {$e->getMessage()}", 500);
  354. }
  355. }
  356. /*** ------------------------------------------------- ------------------------------- * ???? * ---------------- ------------------------------------- -------------- * * ??? ????? ?? ???? * * @usage = $this->mongo_db->update('foo', $data = array()) ;*/
  357. ?? ?? ????($collection = "", $data = array()) {
  358. if (emptyempty($collection) ) {
  359. $this->error("????? Mongo ???? ???? ?????.", 500);
  360. } if (count($data) == 0 || !is_array($data)) {
  361. $this->error("Mongo ????? ????? ??? ??? ????? ??? ??? ????.", 500 );
  362. } try {
  363. $this->db->{$collection}->update($this->wheres, array('$set' => $data), array(' fsync' => TRUE, '??' => FALSE);
  364. $this->clear();
  365. ??(TRUE);
  366. } catch (MongoCursorException $e) {
  367. $this->error("MongoDB?? ??? ???? ??: {$e->getMessage()}", 500);
  368. }
  369. }
  370. /*** ------------------------------------------------- ------------------ * UPDATE_ALL * --- ------------------------------------- -------------- * * ??? ???? ? ?? ?? * * @usage = $this->mongo_db->update_all('foo', $data = array() );*/
  371. public function update_all($collection = "", $data = array()) {
  372. if (emptyempty( $collection)) {
  373. $this->error("????? Mongo ???? ???? ?????.", 500);
  374. } if (count($data) == 0 || !is_array($data)) {
  375. $this->error("Mongo ????? ????? ??? ??? ????? ??? ??? ????.", 500 );
  376. } try {
  377. $this->db->{$collection}->update($this->wheres, array('$set' => $data), array(' fsync' => TRUE, '??' =>
  378. ??(TRUE);
  379. } catch (MongoCursorException $e) {
  380. $this->error("MongoDB?? ??? ???? ??: {$e->getMessage()}", 500);
  381. }
  382. }
  383. /*** ------------------------------------------------- ------------------------------- * ?? * ---------------- ------------------------------------- -------------- * * ?? ??? ?? ??? ????? ?? ?? * * @usage = $this->mongo_db->delete('foo', $data = array ());*/
  384. ?? ?? delete($collection = "") {
  385. if (emptyempty($collection)) {
  386. $this->error("??? Mongo ???? ???? ?????.", 500);
  387. } try {
  388. $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => ??));
  389. $this->clear();
  390. ??(TRUE);
  391. } catch (MongoCursorException $e) {
  392. $this->error("MongoDB?? ??? ?? ??: {$e->getMessage()}", 500);
  393. }
  394. }
  395. /*** ------------------------------------------------- ------------------ * DELETE_ALL * --- ------------------------------------- -------------- * * ?? ??? ?? ??? ????? ?? ?? ?? * * @usage = $this->mongo_db->delete_all('foo', $data = ??());*/
  396. public function delete_all($collection = "") {
  397. if (emptyempty($collection)) {
  398. $this->error( "??? Mongo ???? ???? ?????.", 500);
  399. } try {
  400. $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => ??));
  401. ??(TRUE);
  402. } catch (MongoCursorException $e) {
  403. $this->error("MongoDB?? ??? ?? ??: {$e->getMessage()}", 500);
  404. }
  405. }
  406. /*** ------------------------------------------------- ------------------ * ADD_INDEX * --- ------------------------------------- -------------- * * ??? ????? ???? ???? ? ???? ?????. ?? ?????? ????? * -1, FALSE, 'desc' ?? 'DESC' ?? ???? ???. ??? ??? * 1(ASC)? ?????. * * @usage = $this->mongo_db->add_index($collection, array('first_name' => 'ASC', 'last_name' => -1), array('unique' => TRUE )));*/
  407. ?? ?? add_index($collection = "", $keys = array(), $options = array()) {
  408. if (emptyempty($collection)) {
  409. $this->error("???? ??? Mongo ???? ???? ?????.", 500);
  410. } if (emptyempty($keys) || !is_array($keys)) {
  411. $this->error("?? ???? ??? ??? MongoDB ???? ???? ??? ? ????.", 500);
  412. } foreach ($keys as $col => $val) {
  413. if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
  414. $keys[$col] = -1;
  415. } else {
  416. $keys[$col] = 1;
  417. }
  418. } if ($this->db->{$collection}->ensureIndex($keys, $options) == TRUE) {
  419. $this->clear() ;
  420. return($this);
  421. } else {
  422. $this->error("MongoDB ???? ???? ????? ? ? ??? ??????.", 500);
  423. }
  424. }
  425. /*** ------------------------------------------------- ------------------ * REMOVE_INDEX * --- ------------------------------------- -------------- * * ???? ?? ?? ???? ?????. ?? ?????? ????? * -1, FALSE, 'desc' ?? 'DESC' ?? ???? ???. ??? ??? * 1(ASC)? ?????. * * @usage = $this->mongo_db->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1));*/
  426. ?? ?? Remove_index($collection = "", $keys = array()) {
  427. if (emptyempty( $collection)) {
  428. $this->error("???? ??? Mongo ???? ???? ?????.", 500);
  429. } if (emptyempty($keys) || !is_array($keys)) {
  430. $this->error("??? ?? ?? ??? MongoDB ????? ???? ??? ? ????.", 500);
  431. } if ($this->db->{$collection}->deleteIndex($keys, $options) == TRUE) {
  432. $this->clear();
  433. return($this);
  434. } else {
  435. $this->error("MongoDB ????? ???? ????? ? ? ??? ??????.", 500);
  436. }
  437. }
  438. /*** ------------------------------------------------- ------------------ * REMOVE_ALL_INDEXES * --- ------------------------------------- -------------- * * ????? ?? ???? ?????. * * @usage = $this->mongo_db->remove_all_index($collection);*/
  439. ?? ?? Remove_all_indexes($collection = "") {
  440. if (emptyempty($collection)) {
  441. $this->error("?? ???? ???? ?? ??? Mongo ???? ????.", 500);
  442. } $this->db->{$collection}->deleteIndexes();
  443. $this->clear();
  444. return($this);
  445. }
  446. /*** ------------------------------------------------- ------------------ * LIST_INDEXES * --- ------------------------------------- -------------- * * ???? ?? ???? ?????. * * @usage = $this->mongo_db->list_indexes($collection);*/
  447. public function list_indexes($collection = "") {
  448. if (emptyempty($collection)) {
  449. $this->error( "?? ???? ????? ??? Mongo ???? ????.", 500);
  450. } return($this->db->{$collection}->getIndexInfo());
  451. }
  452. /*** ------------------------------------------------- ------------------ * ?? ??? * --------------- ------------------------------------- --------------- * * ???????? ??? ???? ?????. * ?? ???? ?? ? ??? ??? ? ???? ?????!*/
  453. ?? ?? drop_collection($collection = "") {
  454. if (emptyempty($collection)) {
  455. $this- >error("???????? ????? ??? Mongo ???? ????.", 500);
  456. } $this->db->{$collection}->drop();
  457. TRUE? ?????.
  458. }
  459. /*** ------------------------------------------------- ------------------------------- * ??? * ---------------- ------------------------------------- -------------- * * ??? ??? ?? ???? ??????.*/
  460. ?? ??clear() {
  461. $this->selects = array();
  462. $this->wheres = ??();
  463. $this->limit = NULL;
  464. $this->offset = NULL;
  465. $this->sorts = ??();
  466. }
  467. /*** ------------------------------------------------- ------------------ * ??? ????? ?? * --------------- ------------------------------------- --------------- * * $wheres array()? ??? ????? ?????.*/
  468. ??? ?? where_init($param) {
  469. if (!isset($this->wheres[$param])) {
  470. $this->wheres[$param] = ??();
  471. }
  472. }
  473. ?? ?? ??($str, $t) {
  474. echo $str;
  475. ??;
  476. }
  477. }
  478. ?>
復(fù)代碼

使用范例
  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, ????


本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應(yīng)的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

對基於PHP的API進行版本控制的最佳實踐是什麼? 對基於PHP的API進行版本控制的最佳實踐是什麼? Jun 14, 2025 am 12:27 AM

基於toversionaphp,useUrl deuseUrl specteringforclarityAndEsofRouting,單獨的codetoavoidConflicts,dremecateOldVersionswithClearCommunication,andConsiderCustomHeadeSerlySerallyWhennEnncelsy.startbyplacingtheversionIntheUrl(E.G.,epi/api/v

如何在PHP中實施身份驗證和授權(quán)? 如何在PHP中實施身份驗證和授權(quán)? Jun 20, 2025 am 01:03 AM

tosecurelyhandleauthenticationandationallizationInphp,lofterTheSesteps:1.AlwaysHashPasswordSwithPassword_hash()andverifyusingspasspassword_verify(),usepreparedStatatementStopreventsqlineptions,andStoreSeruserDatain usseruserDatain $ _sessiveferterlogin.2.implementrole-2.imaccessccsccccccccccccccccccccccccc.

PHP中的程序和麵向?qū)ο蟮木幊坦犂g有什麼區(qū)別? PHP中的程序和麵向?qū)ο蟮木幊坦犂g有什麼區(qū)別? Jun 14, 2025 am 12:25 AM

procemal and object-tiriendedprogromming(oop)inphpdiffersimplessintustructure,可重複使用性和datahandling.1.procedural-Progrogursmingusesfunctimesfunctionsormanized sequalized sequalized sequiential,poiperforsmallscripts.2.OpporganizesCodeOrganizescodeOdeIntsocloceSandObjects,ModelingReal-Worlden-Worlden

PHP中有哪些弱參考(弱圖),何時有用? PHP中有哪些弱參考(弱圖),何時有用? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

如何在PHP中安全地處理文件上傳? 如何在PHP中安全地處理文件上傳? Jun 19, 2025 am 01:05 AM

要安全處理PHP中的文件上傳,核心在於驗證文件類型、重命名文件並限制權(quán)限。 1.使用finfo_file()檢查真實MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機文件名,存儲至非Web根目錄;3.通過php.ini和HTML表單限製文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強安全性。這些步驟有效防止安全漏洞,確保文件上傳過程安全可靠。

如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? 如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? Jun 19, 2025 am 01:07 AM

是的,PHP可以通過特定擴展或庫與MongoDB和Redis等NoSQL數(shù)據(jù)庫交互。首先,使用MongoDBPHP驅(qū)動(通過PECL或Composer安裝)創(chuàng)建客戶端實例並操作數(shù)據(jù)庫及集合,支持插入、查詢、聚合等操作;其次,使用Predis庫或phpredis擴展連接Redis,執(zhí)行鍵值設(shè)置與獲取,推薦phpredis用於高性能場景,Predis則便於快速部署;兩者均適用於生產(chǎn)環(huán)境且文檔完善。

PHP中==(鬆散比較)和===(嚴格的比較)之間有什麼區(qū)別? PHP中==(鬆散比較)和===(嚴格的比較)之間有什麼區(qū)別? Jun 19, 2025 am 01:07 AM

在PHP中,==與===的主要區(qū)別在於類型檢查的嚴格程度。 ==在比較前會進行類型轉(zhuǎn)換,例如5=="5"返回true,而===要求值和類型都相同才會返回true,例如5==="5"返回false。使用場景上,===更安全應(yīng)優(yōu)先使用,==僅在需要類型轉(zhuǎn)換時使用。

如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? 如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? Jun 19, 2025 pm 05:13 PM

PHP中使用基本數(shù)學(xué)運算的方法如下:1.加法用 號,支持整數(shù)和浮點數(shù),也可用於變量,字符串數(shù)字會自動轉(zhuǎn)換但不推薦依賴;2.減法用-號,變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號,適用於數(shù)字及類似字符串;4.除法用/號,需避免除以零,並註意結(jié)果可能是浮點數(shù);5.取模用%號,可用於判斷奇偶數(shù),處理負數(shù)時餘數(shù)符號與被除數(shù)一致。正確使用這些運算符的關(guān)鍵在於確保數(shù)據(jù)類型清晰並處理好邊界情況。

See all articles