MongoDB查询类;PHP7+版本使用
连接类:
class Mongo_db {
private $CI;
private $config_file = 'mongodb';
private $connection;
private $db;
private $host;
private $port;
private $user;
private $pass;
private $dbname;
public function __construct()
{
$this->CI = & get_instance();
$this->connection();
$this->connect();
}
//$gt 大于,$gte 大於等於,$lt 小於,$lte 小於等於,,$ne 不等於,
//介於 array("number"=>array('$gt' => 1,'$lt' => 9));
//$in 等於哪些,array("number"=>array('$in' => array(1,2,9)));
//$nin 不等於哪些,array("number"=>array('$nin' => array(1,2,9)));
// 使用正規查詢 array("name" => new MongoRegex("/shi/$i"));
// 或查询 array('$or' => array(array('number'=>2),array('number'=>9)));
/**
* 查询数据
* @param array $query [description]
* @param array $fields [description]
* @param [type] $collection [description]
* @param array $sort [description]
* @param integer $limit [description]
* @param integer $skip [description]
* @return [type] [description]
*/
public function find($query = array(), $fields = array(), $collection, $sort = array(), $limit = 0, $skip = 0) {
if (empty($this->conn)) {
return false;
}
try {
$data = array();
$options = array();
if (!empty($query)) {
$options['projection'] = array_fill_keys($fields, 1);
$options['projection']['_id'] = 0;
}
if (!empty($sort)) {
$options['sort'] = $sort;
}
if (!empty($limit)) {
$options['skip'] = $skip;
$options['limit'] = $limit;
}
$mongoQuery = new MongoDB\Driver\Query($query, $options);
$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
$cursor = $this->conn->executeQuery($this->dbname.'.'.$collection, $mongoQuery, $readPreference);
foreach($cursor as $value) {
$data[] = (array)$value;
}
return $data;
} catch (Exception $e) {
//记录错误日志
}
return false;
}
/**
* 插入
* @param [type] $addArr [description]
* @param [type] $collection [description]
* @return [type] [description]
*/
public function insert($addArr, $collection) {
if (empty($addArr) || !is_array($addArr)) {
return false;
}
if (empty($this->conn)) {
return false;
}
try {
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert($addArr);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 6000);
$result = $this->conn->executeBulkWrite($this->dbname.'.'.$collection, $bulk, $writeConcern);
if ($result->getInsertedCount()) {
return true;
}
} catch (Exception $e) {
//记录错误日志
}
return false;
}
/**
* 删除
* @param [type] $whereArr [description]
* @param array $options [description]
* @param [type] $collection [description]
* @return [type] [description]
*/
public function delete($whereArr, $options = array(), $collection) {
if (empty($whereArr)) {
return false;
}
if (!isset($options['justOne'])) {
$options = array(
'justOne' => false,
);
}
if (empty($this->conn)) {
return false;
}
try {
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete($whereArr, $options);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 30000);
$result = $this->conn->executeBulkWrite($this->dbname.'.'.$collection, $bulk, $writeConcern);
return true;
} catch (Exception $e) {
//记录错误日志
}
return false;
}
public function count($query, $collection) {
try {
$params = array(
'count' => $collection,
'query' => $query,
);
$res = $this->command($params);
$result = $res->toArray();
return $result[0]->n;
} catch (Exception $e) {
//记录错误
}
return false;
}
/**
* 聚合distinct
* @param [type] $key [description]
* @param [type] $where [description]
* @param [type] $collection [description]
* @return [type] [description]
*/
public function distinct($key, $where, $collection) {
try {
$cmd = array(
'distinct' => $collection,
'key' => $key,
'query' => $where,
);
$res = $this->command($cmd);
$result = $res->toArray();
return $result[0]->values;
} catch (Exception $e) {
//记录错误
}
return false;
}
/**
* 执行command操作
* @param [type] $params [description]
* @param [type] $dbName [description]
* @return [type] [description]
*/
public function command($params) {
if (empty($this->conn)) {
return false;
}
try {
$cmd = new MongoDB\Driver\Command($params);
$result = $this->conn->executeCommand($this->dbname, $cmd);
return $result;
} catch (Exception $e) {
//记录错误
}
return false;
}
/**
* 连接MongoDB
* @return [type] [description]
*/
private function connect() {
try{
$connStr = "mongodb://" . $this->host . ":" . $this->port;
$this->conn = new MongoDB\Driver\Manager($connStr);
}
catch(Exception $e){
return false;
}
}
/**
* [connection description]
* @return [type] [description]
*/
private function connection()
{
//按需求赋值
// $this->host = trim($this->host);
// $this->port = trim($this->port);
// $this->user = trim($this->user);
// $this->pass = trim($this->pass);
// $this->dbname = trim($this->dbname);
if (empty($this->host))
{
show_error("The Host must be set to connect to MongoDB", 500);
}
if (empty($this->dbname))
{
show_error("The Database must be set to connect to MongoDB", 500);
}
}
}
使用类
结合MongoDB&&PHP多进程&&Redis的使用类
对MongoDB所有Collection进行数据查询功能
class Mongo_pctl_redis
{
private $RedisName = 'insert_mongodb';
public function __construct()
{
// Do something with $params
parent::__construct();
}
/**
* 读取mongodb数据存入redis
* @return [type] [description]
*/
public function index()
{
$this->load->library('Mongo_db');
$mongodb = new Mongo_db();
$data = $mongodb->command(['listCollections' => 1]);
//获取所有collections写入redis
foreach ($data as $key => $value) {
if($value->name){
RedisDB::lpush($this->RedisName,$value->name);
}
}
}
/**
* 开启多进程执行数据
* @return [type] [description]
*/
public function pcntlbig()
{
//判断redis内是否存在数据
$list_num = RedisDB::lLen($this->RedisName);
if($list_num < 1){
exit;
}
//'开始执行'
for ($i = 0; $i < 10; $i++){
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} elseif ($pid) {
//'进程id:'.$pid
} else {
self::get_big_data();
exit;// 一定要注意退出子进程,否则pcntl_fork() 会被子进程再fork,带来处理上的影响。
}
}
// 等待子进程执行结
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
}
}
/**
* 进程执行内容,从redis拉取数据,按条件搜索MongoDB,搜索结果在存入redis
* @param string $mongodb [description]
* @param string $filepath [description]
* @param string $redis [description]
* @return [type] [description]
*/
public function get_big_data($mongodb='', $filepath='', $redis='')
{
if(!$filepath){
$redis = redisdb();
$this->load->library('Mongo_db');
}
$name = $redis->lPop($this->RedisName);
if($name){
if(!$mongodb){
$mongodb = new Mongo_db();
}
$select = array(
//需要列
);
//搜索条件
$filter = array(
"filter1" => array('$gt' => "0"),
"filter2" => array('$gt' => "0.2"),
);
$sort = array(
//排序字段
'sort' => 1
);
$limit = 10;
$r = $mongodb->find($filter, $select, $name, $sort, $limit);
if($r){
self::dataSave($r, $filepath, $redis);
unset($r);
}
//循环拉取redis内数据做处理
self::get_big_data($mongodb, $filepath, $redis);
}
}
/**
* 数据存储
* @param array $data [description]
* @return [type] [description]
*/
public function dataSave($data=array(), $filepath, $redis)
{
foreach ($data as $key => $value) {
$r = $redis->lPush('bigDataAllInfo', json_encode($value));
}
}
}