<?php
/**
* @desc php mongodb扩展操作类
* @Author:show
*/
namespace mongox;
use MongoDB\BSON\Regex;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Driver\Exception\Exception as MongoDBDriverException;
class CreateCollection {
protected $cmd = array();
function __construct($collectionName) {
$this->cmd["create"] = (string)$collectionName;
}
function setAutoIndexId($bool) {
$this->cmd["autoIndexId"] = (bool)$bool;
}
function setCappedCollection($maxBytes, $maxDocuments = false) {
$this->cmd["capped"] = true;
$this->cmd["size"] = (int)$maxBytes;
if ($maxDocuments) {
$this->cmd["max"] = (int)$maxDocuments;
}
}
function usePowerOf2Sizes($bool) {
if ($bool) {
$this->cmd["flags"] = 1;
} else {
$this->cmd["flags"] = 0;
}
}
function setFlags($flags) {
$this->cmd["flags"] = (int)$flags;
}
function getCommand() {
return new Command($this->cmd);
}
function getCollectionName() {
return $this->cmd["create"];
}
}
/**
* php mongodb扩展操作类
* Author:show
*/
class db_mongodb implements mongo_base
{
//conn相当于manager
public $conn;
public $dbname;
public $collection;
public function __construct($host,$username='',$password='',$dbname='')
{
self::init_mongo($host,$username,$password,$dbname);
}
/**
* 初始化mongo
*/
public function init_mongo($host,$username='',$password='',$dbname='')
{
$authString = "";
if ($username && $password) {
$authString = "{$username}:{$password}@";
}
// $replicaSet && $replicaSet = '?replicaSet='.$replicaSet;
$dsn = "mongodb://{$authString}{$host}";
$this->conn = new Manager($dsn);
if($dbname)
{
$this->dbname = $dbname;
}
}
/**
* @desc 创建集合
* @param collection_name 集合名称
* @param size 大小
*/
public function createCollection($collection_name,$size='')
{
$createCollection = new CreateCollection($collection_name);
if(!empty(MONGOX_SIZE) && empty($size))
{
$size = MONGOX_SIZE;
}
if(!empty($size))
{
$createCollection->setCappedCollection($size);
}
try {
$command = $createCollection->getCommand();
//选择数据库,执行命令
$cursor = $this->conn->executeCommand($this->dbname, $command);
$response = $cursor->toArray()[0];
$collstats = ["collstats" => $createCollection->getCollectionName()];
$cursor = $this->conn->executeCommand($this->dbname, new Command($collstats));
$response = $cursor->toArray()[0];
} catch(MongoDBDriverException $e) {
// echo $e->getMessage(), "\n";
}
}
/**
* @desc 选择集合
* @param collection_name 集合名
*/
public function selectCollection($collection_name)
{
// return $this->conn->selectCollection($collection_name);
$this->collection = $collection_name;
}
/**
* @desc 选择数据库
* @param dbname 数据库名称
*/
public function select($dbname='db')
{
$this->dbname = $dbname;
}
/**
* @desc 获取信息
*/
public function get_one($collection_name='', $condition=array(), $fields=array(), $key='')
{
$filter = array();
$query = new Query($filter, $condition);
$dbcollection = $this->dbname.".".$collection_name;
$row = $this->conn->executeQuery($dbcollection, $query);
return $row;
}
/**
* @desc 插入数据
*/
public function insert($collection_name='', $docarray=array(), $options=array())
{
$bulk = new BulkWrite(['ordered' => true]);
//判断是否有三层
//为空就没必要插入了
if(empty($docarray))
{
return '';
}
$bulk->insert($docarray);
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000);
try {
$dbcollection = $this->dbname.".".$collection_name;
$result = $this->conn->executeBulkWrite($dbcollection, $bulk, $writeConcern);
} catch (BulkWriteException $e) {
$result = $e->getWriteResult();
/*
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n",
$writeConcernError->getMessage(),
$writeConcernError->getCode(),
var_export($writeConcernError->getInfo(), true)
);
}
// Check if any write operations did not complete at all
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n",
$writeError->getIndex(),
$writeError->getMessage(),
$writeError->getCode()
);
}
*/
} catch (MongoDBDriverException $e) {
printf("mongo error!");
// printf("Other error: %s\n", $e->getMessage());
}
// $result->getInsertedCount()
// $result->getModifiedCount()
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mongox-master.zip (6个子文件)
mongox-master
src
mongodb.config.php 432B
db_mongo.php 3KB
mongoadapter.php 2KB
log.php 2KB
db_mongodb.php 6KB
composer.json 436B
共 6 条
- 1
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6742
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功