<?php
/*** Mongodb类** examples:
* $mongo = new HMongodb("127.0.0.1:11223");
* $mongo->selectDb("test_db");
* 创建索引
* $mongo->ensureIndex("test_table", array("id"=>1), array('unique'=>true));
* 获取表的记录
* $mongo->count("test_table");
* 插入记录
* $mongo->insert("test_table", array("id"=>2, "title"=>"asdqw"));
* 更新记录
* $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"));
* 更新记录-存在时更新,不存在时添加-相当于set
* $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"),array("upsert"=>1));
* 查找记录
* $mongo->find("c", array("title"=>"asdqw"), array("start"=>2,"limit"=>2,"sort"=>array("id"=>1)))
* 查找一条记录
* $mongo->findOne("$mongo->findOne("ttt", array("id"=>1))", array("id"=>1));
* 删除记录
* $mongo->remove("ttt", array("title"=>"bbb"));
* 仅删除一条记录
* $mongo->remove("ttt", array("title"=>"bbb"), array("justOne"=>1));
* 获取Mongo操作的错误信息
* $mongo->getError();
*/
class HMongodb {
private $mongo; //Mongodb连接
private $curr_db_name;
private $curr_table_name;
private $error;
public function getInstance($mongo_server, $flag=array())
{
static $mongodb_arr;
if (empty($flag['tag']))
{
$flag['tag'] = 'default'; }
if (isset($flag['force']) && $flag['force'] == true)
{
$mongo = new HMongodb($mongo_server);
if (empty($mongodb_arr[$flag['tag']]))
{
$mongodb_arr[$flag['tag']] = $mongo;
}
return $mongo;
}
else if (isset($mongodb_arr[$flag['tag']]) && is_resource($mongodb_arr[$flag['tag']]))
{
return $mongodb_arr[$flag['tag']];
}
else
{
$mongo = new HMongodb($mongo_server);
$mongodb_arr[$flag['tag']] = $mongo;
return $mongo;
}
}
/**
* 构造函数
* 支持传入多个mongo_server(1.一个出问题时连接其它的server 2.自动将查询均匀分发到不同server)
*
* 参数:
* $mongo_server:数组或字符串-array("127.0.0.1:1111", "127.0.0.1:2222")-"127.0.0.1:1111"
* $connect:初始化mongo对象时是否连接,默认连接
* $auto_balance:是否自动做负载均衡,默认是
*
* 返回值:
* 成功:mongo object
* 失败:false
*/
public function __construct($mongo_server='', $connect=true, $auto_balance=true)
{
if (is_array($mongo_server))
{
$mongo_server_num = count($mongo_server);
if ($mongo_server_num > 1 && $auto_balance)
{
$prior_server_num = rand(1, $mongo_server_num);
$rand_keys = array_rand($mongo_server,$mongo_server_num);
$mongo_server_str = $mongo_server[$prior_server_num-1];
foreach ($rand_keys as $key)
{
if ($key != $prior_server_num - 1)
{
$mongo_server_str .= ',' . $mongo_server[$key];
}
}
}
else
{
$mongo_server_str = implode(',', $mongo_server);
}
}
else
{
$mongo_server_str = $mongo_server;
}
try {
if($mongo_server){
$this->mongo = new Mongo($mongo_server, array('connect'=>$connect));
}else{
$this->mongo = new Mongo();
}
}
catch (MongoConnectionException $e)
{
$this->error = $e->getMessage();
return false;
}
}
/**
* 连接mongodb server
*
* 参数:无
*
* 返回值:
* 成功:true
* 失败:false
*/
public function connect()
{
try {
$this->mongo->connect();
return true;
}
catch (MongoConnectionException $e)
{
$this->error = $e->getMessage();
return false;
}
}
/**
* select db
*
* 参数:$dbname
*
* 返回值:无
*/
public function selectDb($dbname)
{
$this->curr_db_name = $dbname;
}
/**
* 创建索引:如索引已存在,则返回。
*
* 参数:
* $table_name:表名
* $index:索引-array("id"=>1)-在id字段建立升序索引
* $index_param:其它条件-是否唯一索引等
*
* 返回值:
* 成功:true
* 失败:false
*/
public function ensureIndex($table_name, $index, $index_param=array())
{
$dbname = $this->curr_db_name;
$index_param['safe'] = 1;
try {
$this->mongo->$dbname->$table_name->ensureIndex($index, $index_param);
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
}
}
/**
* 插入记录
*
* 参数:
* $table_name:表名
* $record:记录
*
* 返回值:
* 成功:true
* 失败:false
*/
public function insert($table_name, $record)
{
$dbname = $this->curr_db_name;
try {
$this->mongo->$dbname->$table_name->insert($record, array('safe'=>true));
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
}
}
/**
* 查询表的记录数
*
* 参数:
* $table_name:表名
*
* 返回值:表的记录数
*/
public function count($table_name)
{
$dbname = $this->curr_db_name;
return $this->mongo->$dbname->$table_name->count();
}
/**
* 更新记录
*
* 参数:
* $table_name:表名
* $condition:更新条件
* $newdata:新的数据记录
* $options:更新选择-upsert/multiple
*
* 返回值:
* 成功:true
* 失败:false
*/
public function update($table_name, $condition, $newdata, $options=array())
{
$dbname = $this->curr_db_name;
$options['safe'] = 1;
if (!isset($options['multiple']))
{
$options['multiple'] = 0; }
try {
$this->mongo->$dbname->$table_name->update($condition,array('$set'=> $newdata), $options);
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
}
}
/**
* 删除记录
*
* 参数:
* $table_name:表名
* $condition:删除条件
* $options:删除选择-justOne
*
* 返回值:
* 成功:true
* 失败:false
*/
public function remove($table_name, $condition, $options=array())
{
$dbname = $this->curr_db_name;
$options['safe'] = 1;
try {
$this->mongo->$dbname->$table_name->remove($condition, $options);
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
} }
/**
* 查找记录
*
* 参数:
* $table_name:表名
* $query_condition:字段查找条件
* $result_condition:查询结果限制条件-limit/sort等
* $fields:获取字段
*
* 返回值:
* 成功:记录集
* 失败:false
*/
public function find($table_name, $query_condition, $result_condition=array(), $fields=array())
{
$dbname = $this->curr_db_name;
$cursor = $this->mongo->$dbname->$table_name->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 (MongoConnectionException $e)
{
$this->error = $e->getMessage();
return false;
}
catch (MongoCursorTimeoutException $e)
{
$this->error = $e->getMessage();
return false;
}
return $result;
}
/**
* 查找一条记录
*
* 参数:
* $table_name:表名
* $condition:查找条件
* $fields:获取字段
*
* 返回值:
* 成功:一条记录
* 失败:false
*/
public function findOne($table_name, $condition, $fields=array())
{
$dbname = $this->curr_db_name;
return $this->mongo->$dbname->$table_name->findOne($condition, $fields);
}
/**
* 获取当前错误信息
*
* 参数:无
*
* 返回值:当前错误信息
*/
public function getError()
{
return $this->error;
}
/*** Mongodb类** examples:
* $mongo = new HMongodb("127.0.0.1:11223");
* $mongo->selectDb("test_db");
* 创建索引
* $mongo->ensureIndex("test_table", array("id"=>1), array('unique'=>true));
* 获取表的记录
* $mongo->count("test_table");
* 插入记录
* $mongo->insert("test_table", array("id"=>2, "title"=>"asdqw"));
* 更新记录
* $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"));
* 更新记录-存在时更新,不存在时添加-相当于set
* $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"),array("upsert"=>1));
* 查找记录
* $mongo->find("c", array("title"=>"asdqw"), array("start"=>2,"limit"=>2,"sort"=>array("id"=>1)))
* 查找一条记录
* $mongo->findOne("$mongo->findOne("ttt", array("id"=>1))", array("id"=>1));
* 删除记录
* $mongo->remove("ttt", array("title"=>"bbb"));
* 仅删除一条记录
* $mongo->remove("ttt", array("title"=>"bbb"), array("justOne"=>1));
* 获取Mongo操作的错误信息
* $mongo->getError();
*/
}

mongodb数据库简单类的更多相关文章

  1. MongoDB数据库简单操作

    之前学过的有mysql数据库,现在我们学习一种非关系型数据库 一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数 ...

  2. <day001>存储到Mysql、mongoDB数据库+简单的Ajax请求+os模块+进程池+MD5

    任务1:记住如何存储到Mysql.mongoDB数据库 ''' 存储到Mysql ''' import pymysql.cursors class QuotePipeline(object): def ...

  3. android开发学习 ------- MongoDB数据库简单理解

    首先说一下MongoDB是什么? MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. MongoDB 是一个基于分布式文件存储的数据库. N ...

  4. 在IDEA中用三个jar包链接MongoDB数据库——实现增删改查

    安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 使用Robo 3T操作Mon ...

  5. C# Asp.net中简单操作MongoDB数据库(二)

    C# Asp.net中简单操作MongoDB数据库(一)    , mongodb数据库连接可以回顾上面的篇幅. 1.model类: public class BaseEntity { /// < ...

  6. C# Asp.net中简单操作MongoDB数据库(一)

    需要引用MongoDB.Driver.dll.MongoDB.Driver.core.dll.MongoDB.Bson.dll三个dll. 1.数据库连接: public class MongoDb ...

  7. PHP使用MongoDB类操作MongoDB数据库总结

    参考:https://www.php.net/manual/zh/class.mongodb-driver-manager.php 参考:https://www.zhaokeli.com/articl ...

  8. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~

    最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...

  9. DataAccess通用数据库访问类,简单易用,功能强悍

    以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数.支持多事务提交.支持参数复用.支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展dat ...

随机推荐

  1. IDEA注释设置:从当前鼠标位置开始注释快捷键

    在写xml或html注释时,经常出现注释出来的时候都是顶格的,前面包含一大段空格,并没有在鼠标位置开始. 可在设置中进行修改,其他代码格式修改方法类似

  2. 浅谈如何爆踩TLEcoders

    对付一些速度比老奶奶都慢的评测姬, 除了超级小的常数,往往还不得不使用一些不算办法的办法 比如说这个让人无语的$ACcoders$的评测姬, 当我们感到代码已经无法再卡常的时候,对人生已经近乎绝望的时 ...

  3. Noip模拟29(瞎眼忌) 2021.8.3

    T1 最长不下降子序列 在此记录自己的瞎眼... 考场上像一个傻$der$,自己为了防范上升序列和不下降序列的不同特意的造了一组$hack$数据来卡自己:(第一行是序列长度,第二行是序列) 6 1 5 ...

  4. Pandas核心用法

    目录 Numpy和Pandas Numpy科学计算 Pandas数据分析 安装jupyter notebook Numpy语法 创建和基本使用 切片索引 布尔索引 对位运算 矩阵的乘除 其他方法 Pa ...

  5. Java I/O框架 - 总结概述

    总结 以下需要重点掌握: 字节流,以下读取结束全部返回-1 字节节点流-访问文件 FileInputStream/FileOutputStream 可以读取任意文件 可以复制图片 读取字符String ...

  6. POJ 2446 Chessboard(二分图最大匹配)

    题意: M*N的棋盘,规定其中有K个格子不能放任何东西.(即不能被覆盖) 每一张牌的形状都是1*2,问这个棋盘能否被牌完全覆盖(K个格子除外) 思路: M.N很小,把每一个可以覆盖的格子都离散成一个个 ...

  7. linux环境下redis安装(redis伪集群搭建)

    redis在linux环境下搭建 1.创建目录 [root@192 local]# mkdir /usr/local/redis 2.下载redis,并解压 [root@192 local]# wge ...

  8. 修改linux 两种时间的方法

    1,整理了一下怎么修改linux 两种时间的方法. 硬件时间:hwclock 或者clock,设置的方法是 hwclock --set --date="05/12/2018 12:30:50 ...

  9. SSH 提示密码过期,如何通过 ansible 批量更新线上服务器密码

    起因 线上环境是在内网,登陆线上环境需要使用 VPN + 堡垒机 登陆,但是我日常登陆线上环境都是 VPN + 堡垒机 + Socks5常驻代理,在shell端只需要保存会话,会话使用socks5代理 ...

  10. Qt 项目管理文件(.pro) 详解

    项目文件目录树 在 Qt Creator 中新建一个 Widget Application 项目 samp2_1,在选择窗口基类的页面选择 QWidget 作为窗体基类,并选中"Genera ...