mongodb
This commit is contained in:
parent
c89a01f0ac
commit
080e811055
75
README.md
75
README.md
|
@ -40,77 +40,98 @@ return [
|
|||
# 使用案例
|
||||
|
||||
使用注解,自动加载
|
||||
**\Hyperf\Mongodb\MongoDb**
|
||||
**\Hyperf\Mongodb\Mongodb**
|
||||
```php
|
||||
/**
|
||||
* @Inject()
|
||||
* @var MongoDb
|
||||
* @var Mongodb
|
||||
*/
|
||||
protected $mongoDbClient;
|
||||
protected $mongodb;
|
||||
```
|
||||
|
||||
#### **tips:**
|
||||
查询的值,是严格区分类型,string、int类型的哦
|
||||
|
||||
### 查询一条数据
|
||||
|
||||
```php
|
||||
$where = ['_id' => '1'];
|
||||
$result = $this->$mongodb->findOne('test', $where);
|
||||
```
|
||||
|
||||
### 查询全部数据
|
||||
|
||||
```php
|
||||
$where = ['_id' => '1'];
|
||||
$result = $this->$mongodb->findAll('test', $where);
|
||||
```
|
||||
|
||||
### 查询一条数据(_id自动转对象)
|
||||
|
||||
```php
|
||||
$where = ['_id' => '1'];
|
||||
$result = $this->$mongodb->fetchOne('test', $where);
|
||||
```
|
||||
|
||||
### 查询全部数据(_id自动转对象)
|
||||
|
||||
```php
|
||||
$where = ['_id' => '1'];
|
||||
$result = $this->$mongodb->fetchAll('test', $where);
|
||||
```
|
||||
|
||||
### 分页查询
|
||||
```php
|
||||
$list = $this->$mongodb->fetchPagination('article', 10, 0, ['author' => $author]);
|
||||
```
|
||||
|
||||
### 新增
|
||||
|
||||
单个添加
|
||||
```php
|
||||
$insert = [
|
||||
'account' => '',
|
||||
'_id' => '',
|
||||
'password' => ''
|
||||
];
|
||||
$this->$mongoDbClient->insert('fans',$insert);
|
||||
$this->$mongodb->insert('test',$insert);
|
||||
```
|
||||
|
||||
批量添加
|
||||
```php
|
||||
$insert = [
|
||||
[
|
||||
'account' => '',
|
||||
'_id' => '',
|
||||
'password' => ''
|
||||
],
|
||||
[
|
||||
'account' => '',
|
||||
'_id' => '',
|
||||
'password' => ''
|
||||
]
|
||||
];
|
||||
$this->$mongoDbClient->insertAll('fans',$insert);
|
||||
```
|
||||
|
||||
### 查询
|
||||
|
||||
```php
|
||||
$where = ['account'=>'1112313423'];
|
||||
$result = $this->$mongoDbClient->fetchAll('fans', $where);
|
||||
```
|
||||
|
||||
### 分页查询
|
||||
```php
|
||||
$list = $this->$mongoDbClient->fetchPagination('article', 10, 0, ['author' => $author]);
|
||||
$this->$mongodb->insertAll('test',$insert);
|
||||
```
|
||||
|
||||
### 更新
|
||||
```php
|
||||
$where = ['account'=>'1112313423'];
|
||||
$where = ['_id'=>'1112313423'];
|
||||
$updateData = [];
|
||||
|
||||
$this->$mongoDbClient->updateColumn('fans', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
|
||||
$this->$mongoDbClient->updateRow('fans',$where,$updateData);// 更新数据满足$where的行的信息成$newObject
|
||||
$this->$mongodb->updateColumn('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
|
||||
$this->$mongodb->updateRow('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject
|
||||
```
|
||||
### 删除
|
||||
|
||||
```php
|
||||
$where = ['account'=>'1112313423'];
|
||||
$where = ['_id'=>'1112313423'];
|
||||
$all = true; // 为false只删除匹配的一条,true删除多条
|
||||
$this->$mongoDbClient->delete('fans',$where,$all);
|
||||
$this->$mongodb->delete('test',$where,$all);
|
||||
```
|
||||
|
||||
### count统计
|
||||
|
||||
```php
|
||||
$filter = ['isGroup' => "0", 'wechat' => '15584044700'];
|
||||
$count = $this->$mongoDbClient->count('fans', $filter);
|
||||
$count = $this->$mongodb->count('test', $filter);
|
||||
```
|
||||
|
||||
|
||||
|
@ -150,5 +171,5 @@ $pipeline= [
|
|||
]
|
||||
];
|
||||
|
||||
$count = $this->$mongoDbClient->command('fans', $pipeline);
|
||||
$count = $this->$mongodb->command('test', $pipeline);
|
||||
```
|
167
src/Mongodb.php
167
src/Mongodb.php
|
@ -23,16 +23,66 @@ class Mongodb
|
|||
*/
|
||||
protected $factory;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $poolName = 'default';
|
||||
|
||||
|
||||
public function __construct(PoolFactory $factory)
|
||||
{
|
||||
$this->factory = $factory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The key to identify the connection object in coroutine context.
|
||||
*/
|
||||
private function getContextKey(): string
|
||||
{
|
||||
return sprintf('mongodb.connection.%s', $this->poolName);
|
||||
}
|
||||
|
||||
|
||||
private function getConnection()
|
||||
{
|
||||
$connection = null;
|
||||
$hasContextConnection = Context::has($this->getContextKey());
|
||||
if ($hasContextConnection) {
|
||||
$connection = Context::get($this->getContextKey());
|
||||
}
|
||||
if (!$connection instanceof MongodbConnection) {
|
||||
$pool = $this->factory->getPool($this->poolName);
|
||||
$connection = $pool->get()->getConnection();
|
||||
}
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回满足filer的一条数据
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function findOne(string $namespace, array $filter = [], array $options = []): array
|
||||
{
|
||||
try {
|
||||
/**
|
||||
* @var $collection MongoDBConnection
|
||||
*/
|
||||
$collection = $this->getConnection();
|
||||
return $collection->executeFindOne($namespace, $filter, $options);
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回满足filer的全部数据
|
||||
*
|
||||
|
@ -42,6 +92,52 @@ class Mongodb
|
|||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function findAll(string $namespace, array $filter = [], array $options = []): array
|
||||
{
|
||||
try {
|
||||
/**
|
||||
* @var $collection MongoDBConnection
|
||||
*/
|
||||
$collection = $this->getConnection();
|
||||
return $collection->executeFindAll($namespace, $filter, $options);
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回满足filer的一条数据(_id为自动转对象)
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function fetchOne(string $namespace, array $filter = [], array $options = []): array
|
||||
{
|
||||
try {
|
||||
/**
|
||||
* @var $collection MongoDBConnection
|
||||
*/
|
||||
$collection = $this->getConnection();
|
||||
return $collection->executeQueryOne($namespace, $filter, $options);
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回满足filer的全部数据(_id自动转对象)
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function fetchAll(string $namespace, array $filter = [], array $options = []): array
|
||||
{
|
||||
try {
|
||||
|
@ -55,6 +151,12 @@ class Mongodb
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 返回满足filer的分页数据
|
||||
*
|
||||
|
@ -232,70 +334,7 @@ class Mongodb
|
|||
}
|
||||
}
|
||||
|
||||
private function getConnection()
|
||||
{
|
||||
$connection = null;
|
||||
$hasContextConnection = Context::has($this->getContextKey());
|
||||
if ($hasContextConnection) {
|
||||
$connection = Context::get($this->getContextKey());
|
||||
}
|
||||
if (!$connection instanceof MongodbConnection) {
|
||||
$pool = $this->factory->getPool($this->poolName);
|
||||
$connection = $pool->get()->getConnection();
|
||||
}
|
||||
return $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* The key to identify the connection object in coroutine context.
|
||||
*/
|
||||
private function getContextKey(): string
|
||||
{
|
||||
return sprintf('mongodb.connection.%s', $this->poolName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回满足filer的全部数据
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function findAll(string $namespace, array $filter = [], array $options = []): array
|
||||
{
|
||||
try {
|
||||
/**
|
||||
* @var $collection MongoDBConnection
|
||||
*/
|
||||
$collection = $this->getConnection();
|
||||
return $collection->executeFindAll($namespace, $filter, $options);
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回满足filer的全部数据
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function findOne(string $namespace, array $filter = [], array $options = []): array
|
||||
{
|
||||
try {
|
||||
/**
|
||||
* @var $collection MongoDBConnection
|
||||
*/
|
||||
$collection = $this->getConnection();
|
||||
return $collection->executeFindOne($namespace, $filter, $options);
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -112,6 +112,38 @@ class MongodbConnection extends Connection implements ConnectionInterface
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询返回结果的一条数据
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function executeFindOne(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
// 查询数据
|
||||
$result = [];
|
||||
try {
|
||||
$options['limit'] = 1;
|
||||
$query = new Query($filter, $options);
|
||||
$cursor = $this->connection->executeQuery($this->config['db'] . '.' . $namespace, $query);
|
||||
foreach ($cursor as $document) {
|
||||
$result = (array)$document;
|
||||
break;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} finally {
|
||||
$this->pool->release($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询返回结果的全部数据
|
||||
*
|
||||
|
@ -121,6 +153,73 @@ class MongodbConnection extends Connection implements ConnectionInterface
|
|||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function executeFindAll(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
// 查询数据
|
||||
$result = [];
|
||||
try {
|
||||
$query = new Query($filter, $options);
|
||||
$cursor = $this->connection->executeQuery($this->config['db'] . '.' . $namespace, $query);
|
||||
foreach ($cursor as $document) {
|
||||
$result[] = (array)$document;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} finally {
|
||||
$this->pool->release($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询返回结果的一条数据(_id自动转对象)
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function executeQueryOne(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) {
|
||||
$filter['_id'] = new ObjectId($filter['_id']);
|
||||
}
|
||||
// 查询数据
|
||||
$result = [];
|
||||
try {
|
||||
$options['limit'] = 1;
|
||||
$query = new Query($filter, $options);
|
||||
$cursor = $this->connection->executeQuery($this->config['db'] . '.' . $namespace, $query);
|
||||
foreach ($cursor as $document) {
|
||||
$document = (array)$document;
|
||||
$document['_id'] = (string)$document['_id'];
|
||||
$result = $document;
|
||||
break;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} finally {
|
||||
$this->pool->release($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询返回结果的全部数据(_id自动转对象)
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function executeQueryAll(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) {
|
||||
|
@ -131,7 +230,6 @@ class MongodbConnection extends Connection implements ConnectionInterface
|
|||
try {
|
||||
$query = new Query($filter, $options);
|
||||
$cursor = $this->connection->executeQuery($this->config['db'] . '.' . $namespace, $query);
|
||||
|
||||
foreach ($cursor as $document) {
|
||||
$document = (array)$document;
|
||||
$document['_id'] = (string)$document['_id'];
|
||||
|
@ -147,6 +245,15 @@ class MongodbConnection extends Connection implements ConnectionInterface
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 返回分页数据,默认每页10条
|
||||
*
|
||||
|
@ -492,68 +599,5 @@ class MongodbConnection extends Connection implements ConnectionInterface
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询返回结果的全部数据
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function executeFindAll(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
// 查询数据
|
||||
$result = [];
|
||||
try {
|
||||
$query = new Query($filter, $options);
|
||||
$cursor = $this->connection->executeQuery($this->config['db'] . '.' . $namespace, $query);
|
||||
|
||||
foreach ($cursor as $document) {
|
||||
$document = (array)$document;
|
||||
$document['_id'] = (string)$document['_id'];
|
||||
$result[] = $document;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} finally {
|
||||
$this->pool->release($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询返回结果的全部数据
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $filter
|
||||
* @param array $options
|
||||
* @return array
|
||||
* @throws MongoDBException
|
||||
*/
|
||||
public function executeFindOne(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
// 查询数据
|
||||
$result = [];
|
||||
try {
|
||||
$query = new Query($filter, $options);
|
||||
$cursor = $this->connection->executeQuery($this->config['db'] . '.' . $namespace, $query);
|
||||
|
||||
foreach ($cursor as $document) {
|
||||
$document = (array)$document;
|
||||
$document['_id'] = (string)$document['_id'];
|
||||
$result = $document;
|
||||
break;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage());
|
||||
} finally {
|
||||
$this->pool->release($this);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user