検索
ホームページバックエンド開発PHPチュートリアルPHP でカプセル化された mongodb 操作クラス

  1. /*
  2. * このテンプレートを変更するには、[ツール] | [ツール] を選択します。テンプレート
  3. * エディタでテンプレートを開きます。
  4. */
  5. class mongo_db {
  6. private $config;
  7. プライベート $connection;
  8. プライベート $db;
  9. プライベート $connection_string;
  10. プライベート $host;
  11. プライベート $ポート;
  12. プライベート $user;
  13. プライベート$パス;
  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. public function __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("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 {
  42. $n = $this->key + $i;
  43. $key = $n >= $num ? $n - $num : $n;
  44. $this->config = $configs['connect'][$key];
  45. $status = $this->connect();
  46. if($status!=false)
  47. {
  48. $keys['mongo_db'] = $key ;
  49. $this->key = $key;
  50. $data = "";
  51. file_put_contents(WHTY_PATH.'configs/double.php', $data, LOCK_EX);
  52. 休憩;
  53. }
  54. }
  55. }
  56. if($status==false)
  57. {
  58. die('mongoDB not connect');
  59. }
  60. }
  61. function __destruct() {
  62. if((IS_NOSQL != 1)){
  63. return;
  64. }
  65. if($this->connection)
  66. {
  67. $this->connection->close();
  68. }
  69. }
  70. /*** -------------------------------------------------- ----------------------------- * MONGODB に接続します * -------------- -------------------------------------------------- ---------------- * * connection_string() メソッドで生成された接続文字列を使用して、 * MongoDB への接続を確立します。 * config ファイルで 'mongo_persist_key' が true に設定されている場合は、永続的な接続を確立します。 すぐに接続を確立したいため、「persist」 * オプションのみを設定できます。*/
  71. プライベート関数 connect() {
  72. $this->connection_string();
  73. $options = array('connect'=>true,'timeout'=>$this->timeout);
  74. {
  75. $this->connection = new Mongo($this->connection_string, $options); を試してください。
  76. $this->db = $this->接続->{$this->dbname};
  77. return($this);
  78. } catch (MongoConnectionException $e) {
  79. false を返します。
  80. }
  81. }
  82. /*** -------------------------------------------------- ----------------------------- * 接続文字列の構築 * -------------- -------------------------------------------------- ---------------- * * 構成ファイルから接続文字列を構築します。*/
  83. プライベート関数 connection_string() {
  84. $this->host = トリム($this->config['hostname']);
  85. $this->port = トリム($this->config['port']);
  86. $this->user = trim($this->config['username']);
  87. $this->pass = トリム($this->config['パスワード']);
  88. $this->dbname = トリム($this->config['データベース']);
  89. $this->persist = trim($this->config['autoconnect']);
  90. $this->persist_key = トリム($this->config['mongo_persist_key']);
  91. $connection_string = "mongodb://";
  92. if (emptyempty($this->host)) {
  93. $this->error("MongoDB に接続するにはホストを設定する必要があります", 500);
  94. } if (emptyempty($this->dbname)) {
  95. $this->error("データベースは MongoDB に接続するように設定する必要があります", 500);
  96. } if (!emptyempty($this->user) && !emptyempty($this->pass)) {
  97. $connection_string .= "{$this->user}:{$this->pass} @";
  98. } if (isset($this->port) && !emptyempty($this->port)) {
  99. $connection_string .= "{$this->host}:{$this->port}" ;
  100. } else {
  101. $connection_string .= "{$this->host}";
  102. } $this->接続文字列 = トリム($接続_文字列);
  103. }
  104. /*** -------------------------------------------------- ----------------------------- * Switch_db * ---------------- -------------------------------------------------- -------------- * * デフォルトのデータベースから別のデータベースに切り替えます*/
  105. public function switch_db($database = '') {
  106. if (emptyempty($database)) {
  107. $this->error("MongoDB データベースを切り替えるには、新しいデータベース名を指定する必要があります", 500);
  108. } $this->dbname = $database;
  109. {
  110. $this->db = $this->接続->{$this->dbname}; を試してください。
  111. 戻り値(TRUE);
  112. } catch (Exception $e) {
  113. $this->error("Mongo データベースを切り替えることができません: {$e->getMessage()}", 500);
  114. }
  115. }
  116. /*** -------------------------------------------------- ----------------------------- * フィールドを選択 * --------------- -------------------------------------------------- --------------- * * クエリ プロセス中にどのフィールドを含めるか、またはどのフィールドを除外するかを決定します。 * 現在、包含と除外を同時に行うことはできないため、 * $includes 配列が $excludes 配列よりも優先されます。 * 除外するフィールドのみを選択したい場合は、$includes を空の array() のままにしておきます。 * * @usage: $this->mongo_db->select(array('foo', 'bar'))->get('foobar');*/
  117. public function select($includes = array(), $excludes = array()) {
  118. if (!is_array($includes)) {
  119. $includes = array ();
  120. }
  121. if (!is_array($excludes)) {
  122. $excludes = array();
  123. }
  124. if (!emptyempty($includes)) {
  125. foreach ($includes as $col) {
  126. $this->selects[$col] = 1;
  127. }
  128. } else {
  129. foreach ($excludes as $col) {
  130. $this->selects[$col] = 0;
  131. }
  132. } return($this);
  133. }
  134. /*** -------------------------------------------------- ----------------------------- * WHERE パラメータ * --------------- -------------------------------------------------- --------------- * * これらの検索パラメータに基づいてドキュメントを取得します。 $wheres 配列は、 * フィールドをキーとし、 * 値を検索条件とする連想配列である必要があります。 * * @usage = $this->mongo_db->where(array('foo' =>'bar'))->get('foobar');*/
  135. public function where($wheres = array()) {
  136. foreach ((array)$wheres as $wh =>$val) {
  137. $this->wheres [$wh] = $val;
  138. } return($this);
  139. }
  140. /*** -------------------------------------------------- ----------------------------- * WHERE_IN パラメータ * --------------- -------------------------------------------------- --------------- * * $field の値が指定された $in array() にあるドキュメントを取得します。 * * @usage = $this->mongo_db->where_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');*/
  141. public function where_in($field = "", $in = array()) {
  142. $this->where_init($field);
  143. $this->wheres[$field]['$in'] = $in;
  144. return($this);
  145. }
  146. /*** -------------------------------------------------- ----------------------------- * WHERE_NOT_IN パラメータ * --------------- -------------------------------------------------- --------------- * * $field の値が指定された $in array() にないドキュメントを取得します。 * * @usage = $this->mongo_db->where_not_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');*/
  147. public function where_not_in($field = "", $in = array()) {
  148. $this->where_init($field);
  149. $this->wheres[$field]['$nin'] = $in;
  150. return($this);
  151. }
  152. /*** -------------------------------------------------- ----------------------------- * パラメータより大きい場合 * ------------- -------------------------------------------------- ----------------- * * $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. return($this);
  157. }
  158. /*** -------------------------------------------------- ----------------------------- * パラメータ以上の場合 * ---------- -------------------------------------------------- -------------------- * * $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. return($this);
  163. }
  164. /*** -------------------------------------------------- ----------------------------- * パラメータ未満の場合 * ------------- -------------------------------------------------- ----------------- * * $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. return($this);
  169. }
  170. /*** -------------------------------------------------- ----------------------------- * パラメータ以下の場合 * ---------- -------------------------------------------------- -------------------- * * $field の値が $x 以下のドキュメントを取得します * * @usage = $this-> mongo_db->where_lte('foo', 20);*/
  171. public function where_lte($field = "", $x) {
  172. $this->where_init($field);
  173. $this->wheres[$field]['$lte'] = $x;
  174. return($this);
  175. }
  176. /*** -------------------------------------------------- ----------------------------- * パラメータの間の場所 * -------------- -------------------------------------------------- ---------------- * * $field の値が $x と $y の間にあるドキュメントを取得します * * @usage = $this->mongo_db->where_between ('foo'、20、30);*/
  177. public function where_between($field = "", $x, $y) {
  178. $this->where_init($field);
  179. $this->wheres[$field]['$gte'] = $x;
  180. $this->wheres[$field]['$lte'] = $y;
  181. return($this);
  182. }
  183. /*** -------------------------------------------------- ----------------------------- * パラメータの間および等しくない場合 * ---------- -------------------------------------------------- -------------------- * * $field の値が $x と $y の間にあるが等しくないドキュメントを取得します * * @usage = $this ->mongo_db->where_between_ne('foo', 20, 30);*/
  184. public function where_between_ne($field = "", $x, $y) {
  185. $this->where_init($field);
  186. $this->wheres[$field]['$gt'] = $x;
  187. $this->wheres[$field]['$lt'] = $y;
  188. return($this);
  189. }
  190. /*** -------------------------------------------------- ----------------------------- * パラメーターと等しくない場合 * ------------ -------------------------------------------------- ------------------ * * $field の値が $x と等しくないドキュメントを取得します * * @usage = $this->mongo_db-> where_between('foo', 20, 30);*/
  191. public function where_ne($field = "", $x) {
  192. $this->where_init($field);
  193. $this->wheres[$field]['$ne'] = $x;
  194. return($this);
  195. }
  196. /*** -------------------------------------------------- ----------------------------- * どこまたは * --------------- -------------------------------------------------- --------------- * * $field の値が 1 つ以上の値に含まれるドキュメントを取得します * * @usage = $this->mongo_db->where_or(' foo', array( 'foo', 'bar', 'blegh' );*/
  197. public function where_or($field = "", $values) {
  198. $this->where_init($field);
  199. $this->wheres[$field]['$or'] = $values;
  200. return($this);
  201. }
  202. /*** -------------------------------------------------- ----------------------------- * どこで * --------------- -------------------------------------------------- --------------- * * 要素が指定された値と一致するドキュメントを取得します * * @usage = $this->mongo_db->where_and( array ( 'foo' => ; 1, 'b' => 'いくつかの例' );*/
  203. public function where_and($elements_values = array()) {
  204. foreach ((array)$elements_values as $element =>$val) {
  205. $this->wheres [$要素] = $val;
  206. } return($this);
  207. }
  208. /*** -------------------------------------------------- ----------------------------- * WHERE MOD * --------------- -------------------------------------------------- --------------- * * ドキュメントを取得します。 where $field % $mod = $result * * @usage = $this->mongo_db->where_mod( 'foo', 10 、1);*/
  209. public function where_mod($field, $num, $result) {
  210. $this->where_init($field);
  211. $this->wheres[$field]['$mod'] = array($num, $result);
  212. return($this);
  213. }
  214. /*** -------------------------------------------------- ----------------------------- * サイズの場所 * --------------- -------------------------------------------------- --------------- * * フィールドのサイズが指定された $size int にあるドキュメントを取得します * * @usage : $this->mongo_db->where_size(' foo', 1)->get('foobar');*/
  215. public function where_size($field = "", $size = "") {
  216. $this->_where_init($field);
  217. $this->wheres[$field]['$size'] = $size;
  218. リターン ($this);
  219. }
  220. /*** -------------------------------------------------- ----------------------------- * パラメータのようなもの * --------------- -------------------------------------------------- --------------- * * $field の (文字列) 値が値に似ているドキュメントを取得します。デフォルトでは * 大文字と小文字を区別しない検索が可能です。 * * @param $flags * 一般的な正規表現フラグを許可します。 * i = 大文字と小文字を区別しない * m = 複数行 * x = コメントを含めることができます * l = ロケール * s = dotall、"."改行を含むすべてに一致します * u = unicode に一致します * * @param $enable_start_wildcard * TRUE 以外に設定すると、開始行文字 "^" が検索値の前に * 追加され、先頭の値の検索のみを表します* 新しい行。 * * @param $enable_end_wildcard * TRUE 以外に設定すると、終了行文字「$」が * 検索値に追加され、 * 行末の値のみを検索することを表します。 * * @usage = $this->mongo_db->like('foo', 'bar', 'im', FALSE, TRUE);*/
  221. public function like($field = "", $value = "", $flags = "i", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE) {
  222. $field = (文字列) トリム($field);
  223. $this->where_init($field);
  224. $value = (文字列) トリム($value);
  225. $value = quotemeta($value);
  226. if ($enable_start_wildcard !== TRUE) {
  227. $value = "^" . $値;
  228. } if ($enable_end_wildcard !== TRUE) {
  229. $value .= "$";
  230. } $regex = "/$value/$flags";
  231. $this->wheres[$field] = new MongoRegex($regex);
  232. return($this);
  233. }
  234. public function wheres($where){
  235. $this->wheres = $where;
  236. }
  237. /*** -------------------------------------------------- ----------------------------- * パラメータによる順序 * -------------- -------------------------------------------------- ---------------- * * 渡されたパラメータに基づいてドキュメントを並べ替えます。値を降順に設定するには、 * -1、FALSE、'desc'、または 'DESC' のいずれかの値を渡す必要があります。それ以外の場合、値は * 1 (ASC) に設定されます。 * * @usage = $this->mongo_db->where_between('foo', 20, 30);*/
  238. public function order_by($fields = array()) {
  239. if (!is_array($fields) || !count($fields)) return ;
  240. foreach ($fields as $col => $val) {
  241. if ($val == -1 || $val === FALSE || strto lower($val) == 'desc') {
  242. $this- > ソート[$col] = -1;
  243. } else {
  244. $this->sorts[$col] = 1;
  245. }
  246. } return($this);
  247. }
  248. /*** -------------------------------------------------- ----------------------------- * 書類の制限 * --------------- -------------------------------------------------- --------------- * * 結果セットを $x 個のドキュメントに制限します * * @usage = $this->mongo_db->limit($x);*/
  249. public function limit($x = 99999) {
  250. if ($x !== NULL && is_numeric($x) && $x >= 1) {
  251. $this ->limit = (int) $x;
  252. } return($this);
  253. }
  254. /*** -------------------------------------------------- ----------------------------- * オフセット書類 * --------------- -------------------------------------------------- --------------- * * $x 個のドキュメントをスキップするために結果セットをオフセットします * * @usage = $this->mongo_db->offset($x);*/
  255. public function offset($x = 0) {
  256. if ($x !== NULL && is_numeric($x) && $x >= 1) {
  257. $this ->オフセット = (int) $x;
  258. } return($this);
  259. }
  260. /*** -------------------------------------------------- ----------------------------- * GET_WHERE * -------------- -------------------------------------------------- -------------- * * 渡されたパラメータに基づいてドキュメントを取得します * * @usage = $this->mongo_db->get_where('foo', array('bar' = >「何か」));*/
  261. public function get_where($collection = "", $where = array(), $limit = 99999, $orderby=array()) {
  262. if (is_array($orderby) ) || !emptyempty($orderby)) {
  263. $order_by = $this->order_by($order_by);
  264. }
  265. return($this->where($where)->limit($limit)->get($collection));
  266. }
  267. public function selectA($collection = "", $limit = 99999, $orderby=array()) {
  268. if(intval($limit) $limit = 999999;
  269. }
  270. $order_by = $this->order_by($orderby);
  271. $re = $this->limit($limit)->get($collection);
  272. $this->clear();
  273. return (配列)$re;
  274. }
  275. public function listinfo($collection = "", $orderby=array(), $page=1, $pagesize=12) {
  276. $page = max(intval($page), 1);
  277. $offset = $pagesize * ($page - 1);
  278. $pagesizes = $offset + $pagesize;
  279. $this->offset($offset);
  280. $order_by = $this->order_by($orderby);
  281. $re = $this->limit($pagesize)->get($collection);
  282. $this->limit(999999);
  283. $count = $this->count($collection);
  284. $this->pages = ページ($count, $page, $pagesize);
  285. return (配列)$re;
  286. }
  287. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * 得る * - - - - - - - - -------------------------------------------------- -------------- * * 渡されたパラメータに基づいてドキュメントを取得します * * @usage = $this->mongo_db->get('foo', array('bar' = >「何か」));*/
  288. public function get($collection = "") {
  289. if (emptyempty($collection)) {
  290. $this->error("MongoDB からドキュメントを取得するため、コレクション名を渡す必要があります", 500);
  291. } $results = array();
  292. $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit )->skip((int) $this->offset)->sort($this->sorts);
  293. $returns = array();
  294. foreach ($documents as $doc): $returns[] = $doc;
  295. エンドフォーリーチ;
  296. return($returns);
  297. }
  298. public function getMy($collection = "") {
  299. if (emptyempty($collection)) {
  300. $this->error("MongoDB からドキュメントを取得するには、コレクション名を渡す必要があります", 500);
  301. } $results = array();
  302. $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit )->skip((int) $this->offset)->sort($this->sorts);
  303. $returns = array();
  304. foreach ($documents as $doc): $returns[] = $doc;
  305. エンドフォーリーチ;
  306. $this ->クリア();
  307. return($returns);
  308. }
  309. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * カウント * - - - - - - - - -------------------------------------------------- -------------- * * 渡されたパラメータに基づいてドキュメントをカウントします * * @usage = $this->mongo_db->get('foo');*/
  310. public function count($collection = "") {
  311. if (emptyempty($collection)) {
  312. $this->error(" MongoDB からのドキュメント、コレクション名を渡す必要があります", 500);
  313. } $count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)->skip( (int) $this->offset)->count();
  314. $this->clear();
  315. return($count);
  316. }
  317. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * 入れる * - - - - - - - - -------------------------------------------------- -------------- * * 渡されたコレクションに新しいドキュメントを挿入します * * @usage = $this->mongo_db->insert('foo', $data = array() );*/
  318. public function insert($collection = "", $data = array(), $name='ID') {
  319. if (emptyempty($collection)) {
  320. $ this->error("挿入する Mongo コレクションが選択されていません", 500);
  321. } if (count($data) == 0 || !is_array($data)) {
  322. $this->error("Mongo コレクションに挿入するもの、または挿入するものが配列ではありません", 500);
  323. } try {
  324. /**
  325. wxcity_base::load_sys_class('whtysqs','',0);
  326. $mongoseq_class = new whtysqs('creaseidsqs');
  327. $re = $mongoseq_class->query("?name=" . $collection . "&opt=put&data=1");
  328. **/
  329. $re = put_sqs('list_mongo_creaseidsqs','1');
  330. if(is_numeric($re)){
  331. $re++;
  332. $data[$name] = intval($re);
  333. }else{
  334. $data[$name] = intval(time());
  335. //die('mongosqs エラー');
  336. }
  337. $this->db->{$collection}->insert($data, array('fsync' => TRUE));
  338. $this->clear();
  339. $data[$name] を返します;
  340. } catch (MongoCursorException $e) {
  341. $this->error("MongoDB へのデータの挿入に失敗しました: {$e->getMessage()}", 500);
  342. }
  343. }
  344. public function insertWithId($collection = "", $data = array()) {
  345. if (emptyempty($collection)) {
  346. $this->error("挿入する Mongo コレクションが選択されていません"、500);
  347. } if (count($data) == 0 || !is_array($data)) {
  348. $this->error("Mongo コレクションに挿入するもの、または挿入するものが配列ではありません", 500);
  349. } try {
  350. $this->db->{$collection}->insert($data, array('fsync' => TRUE));
  351. $this->clear();
  352. 1 を返します。
  353. } catch (MongoCursorException $e) {
  354. $this->error("MongoDB へのデータの挿入に失敗しました: {$e->getMessage()}", 500);
  355. }
  356. }
  357. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * アップデート * - - - - - - - - -------------------------------------------------- -------------- * * 渡されたコレクションにドキュメントを更新します * * @usage = $this->mongo_db->update('foo', $data = array()) ;*/
  358. public function update($collection = "", $data = array()) {
  359. if (emptyempty($collection)) {
  360. $this->error( 「更新する Mongo コレクションが選択されていません」、500);
  361. } if (count($data) == 0 || !is_array($data)) {
  362. $this->error("Mongo コレクションに更新するものがない、または更新するものが配列ではありません", 500);
  363. } try {
  364. $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' =>) ; TRUE、「複数」 => FALSE);
  365. $this->clear();
  366. 戻り値(TRUE);
  367. } catch (MongoCursorException $e) {
  368. $this->error("MongoDB へのデータの更新に失敗しました: {$e->getMessage()}", 500);
  369. }
  370. }
  371. /*** -------------------------------------------------- ----------------------------- * UPDATE_ALL * ---------------- -------------------------------------------------- -------------- * * 渡されたコレクションに新しいドキュメントを挿入します * * @usage = $this->mongo_db->update_all('foo', $data = array() );*/
  372. public function update_all($collection = "", $data = array()) {
  373. if (emptyempty($collection)) {
  374. $this->error (「更新する Mongo コレクションが選択されていません」、500);
  375. } if (count($data) == 0 || !is_array($data)) {
  376. $this->error("Mongo コレクションに更新するものがない、または更新するものが配列ではありません", 500);
  377. } try {
  378. $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' =>) ; TRUE、'複数' =>
  379. 戻り値(TRUE);
  380. } catch (MongoCursorException $e) {
  381. $this->error("MongoDB へのデータの更新に失敗しました: {$e->getMessage()}", 500);
  382. }
  383. }
  384. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * 消去 * - - - - - - - - -------------------------------------------------- -------------- * * 特定の基準に基づいて、渡されたコレクションからドキュメントを削除します * * @usage = $this->mongo_db->delete('foo', $data = array ());*/
  385. public function delete($collection = "") {
  386. if (emptyempty($collection)) {
  387. $this->error("Mongo コレクションが選択されていませんから削除", 500);
  388. } try {
  389. $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => TRUE));
  390. $this->clear();
  391. 戻り値(TRUE);
  392. } catch (MongoCursorException $e) {
  393. $this->error("MongoDB へのデータの削除に失敗しました: {$e->getMessage()}", 500);
  394. }
  395. }
  396. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * すべて削除 * - - - - - - - - -------------------------------------------------- -------------- * * 特定の基準に基づいて、渡されたコレクションからすべてのドキュメントを削除します * * @usage = $this->mongo_db->delete_all('foo', $data =配列());*/
  397. public function delete_all($collection = "") {
  398. if (emptyempty($collection)) {
  399. $this->error("削除する Mongo コレクションが選択されていません", 500);
  400. } try {
  401. $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => FALSE));
  402. 戻り値(TRUE);
  403. } catch (MongoCursorException $e) {
  404. $this->error("MongoDB へのデータの削除に失敗しました: {$e->getMessage()}", 500);
  405. }
  406. }
  407. /*** -------------------------------------------------- ----------------------------- * 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 ));*/
  408. public function add_index($collection = "", $keys = array(), $options = array()) {
  409. if (emptyempty($collection)) {
  410. $this->error("インデックスを追加する Mongo コレクションが指定されていません", 500);
  411. } if (emptyempty($keys) || !is_array($keys)) {
  412. $this->error("キーが指定されていないため、MongoDB コレクションにインデックスを作成できませんでした", 500);
  413. } foreach ($keys as $col => $val) {
  414. if ($val == -1 || $val === FALSE || strto lower($val) == 'desc') {
  415. $keys [$col] = -1;
  416. } else {
  417. $keys[$col] = 1;
  418. }
  419. } if ($this->db->gt;{$collection}->ensureIndex($keys, $options) == TRUE) {
  420. $this->clear();
  421. return($this);
  422. } else {
  423. $this->error("MongoDB コレクションにインデックスを追加しようとしたときにエラーが発生しました", 500);
  424. }
  425. }
  426. /*** -------------------------------------------------- ----------------------------- * REMOVE_INDEX * ---------------- -------------------------------------------------- -------------- * * コレクション内のキーのインデックスを削除します。値を降順に設定するには、 * -1、FALSE、'desc'、または 'DESC' のいずれかの値を渡す必要があります。それ以外の場合、値は * 1 (ASC) に設定されます。 * * @usage = $this->mongo_db->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1));*/
  427. public function Remove_index($collection = "", $keys = array()) {
  428. if (emptyempty($collection)) {
  429. $this->error ("インデックスを削除する Mongo コレクションが指定されていません", 500);
  430. } if (emptyempty($keys) || !is_array($keys)) {
  431. $this->error("キーが指定されていないため、MongoDB コレクションからインデックスを削除できませんでした", 500);
  432. } if ($this->db->{$collection}->deleteIndex($keys, $options) == TRUE) {
  433. $this->clear();
  434. return($this);
  435. } else {
  436. $this->error("MongoDB コレクションからインデックスを削除しようとしたときにエラーが発生しました", 500);
  437. }
  438. }
  439. /*** -------------------------------------------------- ----------------------------- * REMOVE_ALL_INDEXES * ---------------- -------------------------------------------------- -------------- * * コレクションからすべてのインデックスを削除します。 * * @usage = $this->mongo_db->remove_all_index($collection);*/
  440. public function Remove_all_indexes($collection = "") {
  441. if (emptyempty($collection)) {
  442. $this->error("Mongo コレクションが指定されていませんすべてのインデックスを削除します", 500);
  443. } $this->db->{$collection}->deleteIndexes();
  444. $this->clear();
  445. return($this);
  446. }
  447. /*** -------------------------------------------------- ----------------------------- * LIST_INDEXES * -------------- -------------------------------------------------- -------------- * * コレクション内のすべてのインデックスをリストします。 * * @usage = $this->mongo_db->list_indexes($collection);*/
  448. public function list_indexes($collection = "") {
  449. if (emptyempty($collection)) {
  450. $this->error("すべてのインデックスを削除するための Mongo コレクションが指定されていません"、500);
  451. } return($this->db->{$collection}->getIndexInfo());
  452. }
  453. /*** -------------------------------------------------- ----------------------------- *ドロップコレクション* --------------- -------------------------------------------------- --------------- * * 指定されたコレクションをデータベースから削除します。 * これは実稼働環境で非常に大きな問題を引き起こす可能性があるため、注意してください。*/
  454. public function drop_collection($collection = "") {
  455. if (emptyempty($collection)) {
  456. $this->error("ドロップ元の Mongo コレクションが指定されていませんデータベース」、500);
  457. } $this->db->{$collection}->drop();
  458. TRUE を返します。
  459. }
  460. /*** -------------------------------------------------- - - - - - - - - - - - - - - - - * クリア * - - - - - - - - -------------------------------------------------- -------------- * * クラス変数をデフォルト設定にリセットします*/
  461. プライベート関数 clear() {
  462. $this->selects = array();
  463. $this->wheres = array();
  464. $this->limit = NULL;
  465. $this->オフセット = NULL;
  466. $this->sorts = array();
  467. }
  468. /*** -------------------------------------------------- ----------------------------- * WHERE Initializer * --------------- -------------------------------------------------- --------------- * * $wheres array() に挿入するパラメータを準備します。*/
  469. プライベート関数 where_init($param) {
  470. if (!isset($this->wheres[$param])) {
  471. $this->wheres[$param] ] = 配列();
  472. }
  473. }
  474. public function error($str, $t) {
  475. echo $str;
  476. 終了;
  477. }
  478. }
  479. ?>
复制代

使用例
  1. $table_name=trim(strto lower($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


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
PHPおよびPython:さまざまなパラダイムが説明されていますPHPおよびPython:さまざまなパラダイムが説明されていますApr 18, 2025 am 12:26 AM

PHPは主に手順プログラミングですが、オブジェクト指向プログラミング(OOP)もサポートしています。 Pythonは、OOP、機能、手続き上のプログラミングなど、さまざまなパラダイムをサポートしています。 PHPはWeb開発に適しており、Pythonはデータ分析や機械学習などのさまざまなアプリケーションに適しています。

PHPとPython:彼らの歴史を深く掘り下げますPHPとPython:彼らの歴史を深く掘り下げますApr 18, 2025 am 12:25 AM

PHPは1994年に発信され、Rasmuslerdorfによって開発されました。もともとはウェブサイトの訪問者を追跡するために使用され、サーバー側のスクリプト言語に徐々に進化し、Web開発で広く使用されていました。 Pythonは、1980年代後半にGuidovan Rossumによって開発され、1991年に最初にリリースされました。コードの読みやすさとシンプルさを強調し、科学的コンピューティング、データ分析、その他の分野に適しています。

PHPとPythonの選択:ガイドPHPとPythonの選択:ガイドApr 18, 2025 am 12:24 AM

PHPはWeb開発と迅速なプロトタイピングに適しており、Pythonはデータサイエンスと機械学習に適しています。 1.PHPは、単純な構文と迅速な開発に適した動的なWeb開発に使用されます。 2。Pythonには簡潔な構文があり、複数のフィールドに適しており、強力なライブラリエコシステムがあります。

PHPとフレームワーク:言語の近代化PHPとフレームワーク:言語の近代化Apr 18, 2025 am 12:14 AM

PHPは、多数のWebサイトとアプリケーションをサポートし、フレームワークを通じて開発ニーズに適応するため、近代化プロセスで依然として重要です。 1.PHP7はパフォーマンスを向上させ、新機能を紹介します。 2。Laravel、Symfony、Codeigniterなどの最新のフレームワークは、開発を簡素化し、コードの品質を向上させます。 3.パフォーマンスの最適化とベストプラクティスは、アプリケーションの効率をさらに改善します。

PHPの影響:Web開発などPHPの影響:Web開発などApr 18, 2025 am 12:10 AM

phphassiblasifly-impactedwebdevevermentandsbeyondit.1)itpowersmajorplatformslikewordpratsandexcelsindatabase interactions.2)php'sadaptableability allowsitale forlargeapplicationsusingframeworkslikelavel.3)

スカラータイプ、リターンタイプ、ユニオンタイプ、ヌル可能なタイプなど、PHPタイプのヒントはどのように機能しますか?スカラータイプ、リターンタイプ、ユニオンタイプ、ヌル可能なタイプなど、PHPタイプのヒントはどのように機能しますか?Apr 17, 2025 am 12:25 AM

PHPタイプは、コードの品質と読みやすさを向上させるためのプロンプトがあります。 1)スカラータイプのヒント:php7.0であるため、基本データ型は、int、floatなどの関数パラメーターで指定できます。 3)ユニオンタイプのプロンプト:PHP8.0であるため、関数パラメーターまたは戻り値で複数のタイプを指定することができます。 4)Nullable Typeプロンプト:null値を含めることができ、null値を返す可能性のある機能を処理できます。

PHPは、オブジェクトのクローニング(クローンキーワード)と__Clone Magicメソッドをどのように処理しますか?PHPは、オブジェクトのクローニング(クローンキーワード)と__Clone Magicメソッドをどのように処理しますか?Apr 17, 2025 am 12:24 AM

PHPでは、クローンキーワードを使用してオブジェクトのコピーを作成し、\ _ \ _クローンマジックメソッドを使用してクローン動作をカスタマイズします。 1.クローンキーワードを使用して浅いコピーを作成し、オブジェクトのプロパティをクローン化しますが、オブジェクトのプロパティはクローニングしません。 2。\ _ \ _クローン法は、浅いコピーの問題を避けるために、ネストされたオブジェクトを深くコピーできます。 3.クローニングにおける円形の参照とパフォーマンスの問題を避けるために注意し、クローニング操作を最適化して効率を向上させます。

PHP対Python:ユースケースとアプリケーションPHP対Python:ユースケースとアプリケーションApr 17, 2025 am 12:23 AM

PHPはWeb開発およびコンテンツ管理システムに適しており、Pythonはデータサイエンス、機械学習、自動化スクリプトに適しています。 1.PHPは、高速でスケーラブルなWebサイトとアプリケーションの構築においてうまく機能し、WordPressなどのCMSで一般的に使用されます。 2。Pythonは、NumpyやTensorflowなどの豊富なライブラリを使用して、データサイエンスと機械学習の分野で驚くほどパフォーマンスを発揮しています。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール