加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (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

public function getMy($collection = "") {
if (emptyempty($collection)) {
$this->error("In order to retreive documents from MongoDB,$this->selects)->limit((int) $this->limit)->skip((int) $this->offset)->sort($this->sorts);
$returns = array();
foreach ($documents as $doc): $returns[] = $doc;
endforeach;
$this -> clear();
return($returns);
}

/* -------------------------------------------------------------------------------- COUNT -------------------------------------------------------------------------------- Count the documents based upon the passed parameters @usage = $this->mongo_db->get('foo'); */
public function count($collection = "") {
if (emptyempty($collection)) {
$this->error("In order to retreive a count of documents from MongoDB,500);
} $count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)->skip((int) $this->offset)->count();
$this->clear();
return($count);
}

/ -------------------------------------------------------------------------------- INSERT -------------------------------------------------------------------------------- Insert a new document into the passed collection @usage = $this->mongo_db->insert('foo',$data = array()); /
public function insert($collection = "",$data = array(),$name='ID') {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to insert into",500);
} if (count($data) == 0 || !is_array($data)) {
$this->error("Nothing to insert into Mongo collection or insert is not an array",500);
} try {
/
wxcity_base::load_sys_class('whtysqs','',0);
$mongoseq_class = new whtysqs('creaseidsqs');
$re = $mongoseq_class->query("?name=" . $collection . "&opt=put&data=1");
**/
$re = put_sqs('list_mongo_creaseidsqs','1');
if(is_numeric($re)){
$re++;
$data[$name] = intval($re);
}else{
$data[$name] = intval(time());
//die('mongosqs error');
}
$this->db->{$collection}->insert($data,array('fsync' => TRUE));
$this->clear();
return $data[$name];
} catch (MongoCursorException $e) {
$this->error("Insert of data into MongoDB failed: {$e->getMessage()}",500);
}
}

public function insertWithId($collection = "",$data = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to insert into",500);
} try {
$this->db->{$collection}->insert($data,array('fsync' => TRUE));
$this->clear();
return 1;
} catch (MongoCursorException $e) {
$this->error("Insert of data into MongoDB failed: {$e->getMessage()}",500);
}
}
/* -------------------------------------------------------------------------------- UPDATE -------------------------------------------------------------------------------- Update a document into the passed collection @usage = $this->mongo_db->update('foo',$data = array()); */
public function update($collection = "",$data = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to update",500);
} if (count($data) == 0 || !is_array($data)) {
$this->error("Nothing to update in Mongo collection or update is not an array",500);
} try {
$this->db->{$collection}->update($this->wheres,array('$set' => $data),array('fsync' => TRUE,'multiple' => FALSE));
$this->clear();
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Update of data into MongoDB failed: {$e->getMessage()}",500);
}
}

/* -------------------------------------------------------------------------------- UPDATE_ALL -------------------------------------------------------------------------------- Insert a new document into the passed collection @usage = $this->mongo_db->update_all('foo',$data = array()); */
public function update_all($collection = "",'multiple' => TRUE));
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Update of data into MongoDB failed: {$e->getMessage()}",500);
}
}

(编辑:济源站长网)

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

热点阅读