PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php操作mongodb封装类与用法实例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP操作mongodb封装类与用法分享给大家供大家参,具体如下:

近来学习了mongodb,刚好是做PHP开发的,随便写了PHP操作mongodb的封装类.

@H_757_6@mydb; return self::$conn; } private function __clone(){ trigger_error('Clone is not allowed'); }//禁止克隆 //创建索引 public function ensureIndex($table,$index,$index_param=array()) { $index_param['safe'] = 1; try { self::$mon->$table->ensureIndex($index,$index_param); return true; } catch (MongocursorException $E) { self::$error = $e->getmessage(); return false; } } //添加 public function insert($table,$arr){ try { self::$mon->$table->insert($arr,array('w'=>truE)); return true; } catch (MongocursorException $E) { self::$error = $e->getmessage(); return false; } } //更新 public function update($table,$condition,$new_arr,$options=array()) { $options['w'] = 1; if (!isset($options['multiple'])) { $options['multiple'] = 0; } try { self::$mon->$table->update($condition,$options); return true; } catch (MongocursorException $E) { self::$error = $e->getmessage(); return false; } } //删除 public function remove($table,$options=array()) { $options['w'] = 1; try { self::$mon->$table->remove($condition,$options); return true; } catch (MongocursorException $E) { self::$error = $e->getmessage(); return false; } } //查找 public function find($table,$query_condition,$result_condition=array(),$fields=array()) { $cursor = self::$mon->$table->find($query_condition,$fields); if (!empty($result_condition['start'])) { $cursor->skip($result_condition['start']); } if (!empty($result_condition['limit'])) { $cursor->limit($result_condition['limit']); } if (!empty($result_condition['sort'])) { $cursor->sort($result_condition['sort']); } $result = array(); try { while ($cursor->hasNext()) { $result[] = $cursor->getNext(); } } catch (MongocursorTimeoutException $E) { self::$error = $e->getmessage(); return false; } return $result; } //查找一条记录 public function findOne($table,$fields=array()) { return self::$mon->$table->findOne($condition,$fields); } //返回表的记录数 public function count($tablE) { return self::$mon->$table->count(); } //返回错误信息 public function getError() { return self::$error; } }

大佬总结

以上是大佬教程为你收集整理的php操作mongodb封装类与用法实例全部内容,希望文章能够帮你解决php操作mongodb封装类与用法实例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。