mongoDB操作类PHP代码是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。<?php include_once
mongoDB操作类PHP代码是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
<?php
include_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'Config'.DIRECTORY_SEPARATOR.'database.php';
class MonGoClass3{
static public $conn;//数据库连接
static public $db;//数据库选择
static public $collection;//结果集合
public $debug;
public function __construct($type=0){
$database='';
$database = new DATABASE_CONFIG();
if($type==1){
$mongo_master = $database->mongo_hems3;
}else if($type==2) {
$mongo_master = $database->mongo162;
}else if($type == 3) {
$mongo_master = $database->mongo_yuanchuang;
} else if($type==5) {
$mongo_master = $database->mongo_backup;
}
else if($type == 0){
$mongo_master = $database->mongo;
}
$host_master = $mongo_master['host'];
$database = $mongo_master['database'];
$port = $mongo_master['port'];
$this->debug = true;
// $this->conn = new Mongo("mongodb://${username}:${passWord}@${host}");
// $this->conn = new Mongo("mongodb://${host_master},${host_slave}",array("replicaSet" => "shard"));
try{
$this->conn = new Mongo("mongodb://${host_master}:${port}");
}catch(Exception $e){
$str = $e."\r\n".date('Y-m-d H:i:s',time())." 连接mongo失败\r\n";
$this->getMongoLogo($str);
return false;
}
// $this->conn=$db->admin;
// $this->conn->authenticate('root', '123456');
$this->db = $this->conn->{$database};
}
public function getCount($collection,$conditions = array()){
$this->collection = $this->db->{$collection};
$count = $this->collection->find($conditions)->count();
return $count;
}
public function getData($collection,$conditions = array(),$field=array(),$order=array(),$start=0,$pernum=20){
$this->collection = $this->db->{$collection};
if(empty($collection)){
return false;
}
try {
if($start==1&&$pernum==0){
if($order){
$res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order);
}else{
$res = $this->collection->find($conditions,$field)->slaveOkay(true);
}
}else{
if($order){
$res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order)->skip($start)->limit($pernum);
}else{
$res = $this->collection->find($conditions,$field)->slaveOkay(true)->skip($start)->limit($pernum);
}
}
}catch(Exception $e){
$res = array();
$str = date('Y-m-d H:i:s',time())." 从集合".$collection."获取数据\r\n条件为:\r\n";
$str1= '';
foreach($conditions as $key => $val){
$str1.= $key."------------".$val."\r\n";
}
$this->getMongoLogo($str.$str1."失败\r\n");
return false;
}
try{
$res->rewind();
}catch(Exception $e){
$str = date('Y-m-d H:i:s',time())." 从集合".$collection."获取数据\r\n条件为:\r\n";
$str1= '';
foreach($conditions as $key => $val){
$str1.= $key."------------".$val."\r\n";
}
$this->getMongoLogo($str.$str1."失败\r\n");
return false;
}
if($res->valid()){
return iterator_to_array($res,true);
}else{
return false;
}
}
public function getDataOr($collection,$conditions){
$this->collection = $this->db->{$collection};
$res = $this->collection->find(array('$or'=>$conditions));
return $res;
}
public function getIdDataCount($collection,$conditions = array()){
$this->collection = $this->db->{$collection};
$count = $this->collection->find($conditions)->count();
return $count;
}
public function getIdData($collection,$conditions){
$this->collection = $this->db->{$collection};
$res = $this->collection->find($conditions);
return $res;
}
public function editData($collection,$conditions,$data){
$this->collection = $this->db->{$collection};
if($this->collection->update($conditions ,array('$set'=>$data),array('multiple'=>1))){
$code = 1;
$msg = '修改成功!';
}else{
$code = 0;
$msg = '修改失败!';
}
return array('code'=>$code,'msg'=>$msg);
}
public function delData($collection,$conditions){
$this->collection = $this->db->{$collection};
if($this->collection->remove($conditions)){
$code = 1;
$msg = '删除成功!';
}else{
$code = 0;
$msg = '删除失败!';
}
return array('code'=>$code,'msg'=>$msg);
}
public function save($collection,$field,$flag=0){
$this->collection = $this->db->{$collection};
$log = "";
foreach($field as $kdata => $vdata){
$log .= $kdata.":".$vdata.",";
}
$log = trim($log,",");
$newLog = "\r\n-------\r\ntime:".date("Y-m-d H:i:s")."\r\ndata".$log;
$oldLog = file_get_contents("../tmp/mongoLog.txt");
@file_put_contents("../tmp/mongoLog.txt",$oldLog.$newLog);
if($flag){
$result = $this->collection->save($field,array('safe'=>1));
}else{
$result = $this->collection->insert($field,array('safe'=>1));
}
return $result;
}
}
?>
关于MongoDB操作类PHP代码是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网PHP编程频道了解更多相关知识。
--结束END--
本文标题: MongoDB操作类PHP代码是怎样的
本文链接: https://lsjlt.com/news/61245.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0