加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php封装的mongodb操作类代码

发布时间:2021-02-25 16:12:55 所属栏目:PHP教程 来源:网络整理
导读:核心代码 /* To change this template,choose Tools | Templates and open the template in the editor. */ class mongo_db { private $config; private $connection; private $db; private $connection_string; private $host; private $port; private $use

/* -------------------------------------------------------------------------------- DELETE_ALL -------------------------------------------------------------------------------- Delete all documents from the passed collection based upon certain criteria @usage = $this->mongo_db->delete_all('foo',$data = array()); */
public function delete_all($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to delete from",'justOne' => FALSE));
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Delete of data into MongoDB failed: {$e->getMessage()}",500);
}
}

/* -------------------------------------------------------------------------------- ADD_INDEX -------------------------------------------------------------------------------- Ensure an index of the keys in a collection with optional parameters. To set values to descending order,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)); /
public function add_index($collection = "",$keys = array(),$options = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to add index to",500);
} if (emptyempty($keys) || !is_array($keys)) {
$this->error("Index could not be created to MongoDB Collection because no keys were specified",500);
} foreach ($keys as $col => $val) {
if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
$keys[$col] = -1;
} else {
$keys[$col] = 1;
}
} if ($this->db->{$collection}->ensureIndex($keys,$options) == TRUE) {
$this->clear();
return($this);
} else {
$this->error("An error occured when trying to add an index to MongoDB Collection",500);
}
}

/* -------------------------------------------------------------------------------- REMOVE_INDEX -------------------------------------------------------------------------------- Remove an index of the keys in a collection. To set values to descending order,else they will be set to 1 (ASC). @usage = $this->mongo_db->remove_index($collection,'last_name' => -1)); /
public function remove_index($collection = "",$keys = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to remove index from",500);
} if (emptyempty($keys) || !is_array($keys)) {
$this->error("Index could not be removed from MongoDB Collection because no keys were specified",500);
} if ($this->db->{$collection}->deleteIndex($keys,$options) == TRUE) {
$this->clear();
return($this);
} else {
$this->error("An error occured when trying to remove an index from MongoDB Collection",500);
}
}

/* -------------------------------------------------------------------------------- REMOVE_ALL_INDEXES -------------------------------------------------------------------------------- Remove all indexes from a collection. @usage = $this->mongo_db->remove_all_index($collection); */
public function remove_all_indexes($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to remove all indexes from",500);
} $this->db->{$collection}->deleteIndexes();
$this->clear();
return($this);
}

/* -------------------------------------------------------------------------------- LIST_INDEXES -------------------------------------------------------------------------------- Lists all indexes in a collection. @usage = $this->mongo_db->list_indexes($collection); */
public function list_indexes($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to remove all indexes from",500);
} return($this->db->{$collection}->getIndexInfo());
}

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读