From e85e9b38ba5ba9af82c93c5d518c23c33419159a Mon Sep 17 00:00:00 2001 From: chenyao Date: Tue, 28 Jul 2020 14:38:55 +0800 Subject: [PATCH] 2.1.3 --- README.md | 32 +++++++- src/Mongodb.php | 115 +++++++++++++++++++++++++---- src/MongodbConnection.php | 149 ++++++++++++++++++++++++++++++++++---- 3 files changed, 264 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index fceadea..a6e82f2 100644 --- a/README.md +++ b/README.md @@ -75,19 +75,19 @@ $list = $this->$mongodb->findPagination('article', 10, 0, ['author' => $author]) ```php $where = ['_id' => '1']; -$result = $this->$mongodb->fetchOne('test', $where); +$result = $this->$mongodb->findOneId('test', $where); ``` ### 查询全部数据(_id自动转对象) ```php $where = ['_id' => '1']; -$result = $this->$mongodb->fetchAll('test', $where); +$result = $this->$mongodb->findAllId('test', $where); ``` ### 分页查询(_id自动转对象) ```php -$list = $this->$mongodb->fetchPagination('article', 10, 0, ['author' => $author]); +$list = $this->$mongodb->findPaginationId('article', 10, 0, ['author' => $author]); ``` ### 插入一条数据 @@ -122,11 +122,35 @@ $updateData = []; $this->$mongodb->updateColumn('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段 $this->$mongodb->updateRow('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject ``` + +### 更新(_id自动转对象) +```php +$where = ['_id'=>'1112313423']; +$updateData = []; + +$this->$mongodb->updateColumnId('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段 +$this->$mongodb->updateRowId('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject +``` + ### 删除 ```php $where = ['_id'=>'1112313423']; $all = true; // 为false只删除匹配的一条,true删除多条 -$this->$mongodb->delete('test',$where,$all); +$this->$mongodb->deleteOne('test',$where,$all); +``` + +### 批量删除 +```php +$where = ['_id'=>'1112313423']; +$all = true; // 为false只删除匹配的一条,true删除多条 +$this->$mongodb->deleteMany('test',$where,$all); +``` + +### 删除(_id自动转对象) +```php +$where = ['_id'=>'1112313423']; +$all = true; // 为false只删除匹配的一条,true删除多条 +$this->$mongodb->deleteOneId('test',$where,$all); ``` ### 统计 diff --git a/src/Mongodb.php b/src/Mongodb.php index 9f90c0d..94aaed4 100644 --- a/src/Mongodb.php +++ b/src/Mongodb.php @@ -71,7 +71,7 @@ class Mongodb * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->executeFindOne($namespace, $filter, $options); + return $collection->execFindOne($namespace, $filter, $options); } catch (\Exception $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } @@ -93,7 +93,7 @@ class Mongodb * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->executeFindAll($namespace, $filter, $options); + return $collection->execFindAll($namespace, $filter, $options); } catch (\Exception $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } @@ -132,14 +132,14 @@ class Mongodb * @return array * @throws MongoDBException */ - public function fetchOne(string $namespace, array $filter = [], array $options = []): array + public function findOneId(string $namespace, array $filter = [], array $options = []): array { try { /** * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->executeFetchOne($namespace, $filter, $options); + return $collection->execFindOneId($namespace, $filter, $options); } catch (\Exception $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } @@ -154,14 +154,14 @@ class Mongodb * @return array * @throws MongoDBException */ - public function fetchAll(string $namespace, array $filter = [], array $options = []): array + public function fetchAllId(string $namespace, array $filter = [], array $options = []): array { try { /** * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->executeFetchAll($namespace, $filter, $options); + return $collection->execFindAllId($namespace, $filter, $options); } catch (\Exception $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } @@ -178,14 +178,14 @@ class Mongodb * @return array * @throws MongoDBException */ - public function fetchPagination(string $namespace, int $limit, int $currentPage, array $filter = [], array $options = []): array + public function findPaginationId(string $namespace, int $limit, int $currentPage, array $filter = [], array $options = []): array { try { /** * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->execFetchPagination($namespace, $limit, $currentPage, $filter, $options); + return $collection->execFindPaginationId($namespace, $limit, $currentPage, $filter, $options); } catch (\Exception $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } @@ -219,7 +219,7 @@ class Mongodb * @return bool|string * @throws MongoDBException */ - public function insertAll($namespace, array $data) + public function insertMany($namespace, array $data) { if (count($data) == count($data, 1)) { throw new MongoDBException('data is can only be a two-dimensional array'); @@ -229,7 +229,7 @@ class Mongodb * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->execInsertAll($namespace, $data); + return $collection->execInsertMany($namespace, $data); } catch (MongoDBException $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } @@ -280,22 +280,107 @@ class Mongodb } /** - * 删除满足条件的数据,默认只删除匹配条件的第一条记录,如果要删除多条$limit=true + * 更新数据满足$filter的行的信息成$newObject(_id自动转对象) * - * @param string $namespace + * @param $namespace * @param array $filter - * @param bool $limit + * @param array $newObj * @return bool * @throws MongoDBException */ - public function delete(string $namespace, array $filter = [], bool $limit = false): bool + public function updateRowId($namespace, array $filter = [], array $newObj = []): bool { try { /** * @var $collection MongodbConnection */ $collection = $this->getConnection(); - return $collection->execDelete($namespace, $filter, $limit); + return $collection->execUpdateRowId($namespace, $filter, $newObj); + } catch (\Exception $e) { + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } + } + + /** + * 只更新数据满足$filter的行的列信息中在$newObject中出现过的字段(_id自动转对象) + * + * @param $namespace + * @param array $filter + * @param array $newObj + * @return bool + * @throws MongoDBException + */ + public function updateColumnId($namespace, array $filter = [], array $newObj = []): bool + { + try { + /** + * @var $collection MongodbConnection + */ + $collection = $this->getConnection(); + return $collection->execUpdateColumnId($namespace, $filter, $newObj); + } catch (\Exception $e) { + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } + } + + /** + * 删除满足条件的数据,默认只删除匹配条件的第一条记录,如果要删除多条$limit=true + * + * @param string $namespace + * @param array $filter + * @return bool + * @throws MongoDBException + */ + public function deleteOne(string $namespace, array $filter = []): bool + { + try { + /** + * @var $collection MongodbConnection + */ + $collection = $this->getConnection(); + return $collection->execDeleteOne($namespace, $filter); + } catch (\Exception $e) { + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } + } + + /** + * 删除满足条件的数据,默认只删除匹配条件的第一条记录,如果要删除多条$limit=true + * + * @param string $namespace + * @param array $filter + * @return bool + * @throws MongoDBException + */ + public function deleteMany(string $namespace, array $filter = []): bool + { + try { + /** + * @var $collection MongodbConnection + */ + $collection = $this->getConnection(); + return $collection->execDeleteMany($namespace, $filter); + } catch (\Exception $e) { + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } + } + + /** + * 删除满足条件的数据,默认只删除匹配条件的第一条记录,如果要删除多条$limit=true(_id自动转对象) + * + * @param string $namespace + * @param array $filter + * @return bool + * @throws MongoDBException + */ + public function deleteOneId(string $namespace, array $filter = []): bool + { + try { + /** + * @var $collection MongodbConnection + */ + $collection = $this->getConnection(); + return $collection->execDeleteOneId($namespace, $filter); } catch (\Exception $e) { throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); } diff --git a/src/MongodbConnection.php b/src/MongodbConnection.php index c772c5a..e1ec469 100644 --- a/src/MongodbConnection.php +++ b/src/MongodbConnection.php @@ -181,7 +181,7 @@ class MongodbConnection extends Connection implements ConnectionInterface * @return array * @throws MongoDBException */ - public function executeFindOne(string $namespace, array $filter = [], array $options = []) + public function execFindOne(string $namespace, array $filter = [], array $options = []) { // 查询数据 $result = []; @@ -212,7 +212,7 @@ class MongodbConnection extends Connection implements ConnectionInterface * @return array * @throws MongoDBException */ - public function executeFindAll(string $namespace, array $filter = [], array $options = []) + public function execFindAll(string $namespace, array $filter = [], array $options = []) { // 查询数据 $result = []; @@ -286,7 +286,7 @@ class MongodbConnection extends Connection implements ConnectionInterface * @return array * @throws MongoDBException */ - public function executeFetchOne(string $namespace, array $filter = [], array $options = []) + public function execFindOneId(string $namespace, array $filter = [], array $options = []) { if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { $filter['_id'] = new ObjectId($filter['_id']); @@ -322,7 +322,7 @@ class MongodbConnection extends Connection implements ConnectionInterface * @return array * @throws MongoDBException */ - public function executeFetchAll(string $namespace, array $filter = [], array $options = []) + public function execFindAllId(string $namespace, array $filter = [], array $options = []) { if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { $filter['_id'] = new ObjectId($filter['_id']); @@ -358,7 +358,7 @@ class MongodbConnection extends Connection implements ConnectionInterface * @return array * @throws MongoDBException */ - public function execFetchPagination(string $namespace, int $limit = 10, int $currentPage = 0, array $filter = [], array $options = []) + public function execFindPaginationId(string $namespace, int $limit = 10, int $currentPage = 0, array $filter = [], array $options = []) { if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { $filter['_id'] = new ObjectId($filter['_id']); @@ -437,7 +437,7 @@ class MongodbConnection extends Connection implements ConnectionInterface * @return bool|string * @throws MongoDBException */ - public function execInsertAll(string $namespace, array $data = []) + public function execInsertMany(string $namespace, array $data = []) { try { $bulk = new BulkWrite(); @@ -473,9 +473,6 @@ class MongodbConnection extends Connection implements ConnectionInterface public function execUpdateRow(string $namespace, array $filter = [], array $newObj = []): bool { try { - if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { - $filter['_id'] = new ObjectId($filter['_id']); - } $bulk = new BulkWrite; $bulk->update( $filter, @@ -511,6 +508,83 @@ class MongodbConnection extends Connection implements ConnectionInterface * @throws MongoDBException */ public function execUpdateColumn(string $namespace, array $filter = [], array $newObj = []): bool + { + try { + $bulk = new BulkWrite; + $bulk->update( + $filter, + ['$set' => $newObj], + ['multi' => false, 'upsert' => false] + ); + $written = new WriteConcern(WriteConcern::MAJORITY, 1000); + $result = $this->connection->executeBulkWrite($this->config['db'] . '.' . $namespace, $bulk, $written); + $modifiedCount = $result->getModifiedCount(); + $update = $modifiedCount == 1 ? true : false; + } catch (\Exception $e) { + $update = false; + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } finally { + $this->release(); + return $update; + } + } + + /** + * 数据更新,效果是满足filter的行,只更新$newObj中的$set出现的字段(_id自动转对象) + * http://php.net/manual/zh/mongodb-driver-bulkwrite.update.php + * $bulk->update( + * ['x' => 2], + * ['$set' => ['y' => 3]], + * ['multi' => false, 'upsert' => false] + * ); + * + * @param string $namespace + * @param array $filter + * @param array $newObj + * @return bool + * @throws MongoDBException + */ + public function execUpdateRowId(string $namespace, array $filter = [], array $newObj = []): bool + { + try { + if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { + $filter['_id'] = new ObjectId($filter['_id']); + } + $bulk = new BulkWrite; + $bulk->update( + $filter, + ['$set' => $newObj], + ['multi' => true, 'upsert' => false] + ); + $written = new WriteConcern(WriteConcern::MAJORITY, 1000); + $result = $this->connection->executeBulkWrite($this->config['db'] . '.' . $namespace, $bulk, $written); + $modifiedCount = $result->getModifiedCount(); + $update = $modifiedCount == 0 ? false : true; + } catch (\Exception $e) { + $update = false; + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } finally { + $this->pool->release($this); + return $update; + } + } + + /** + * 数据更新, 效果是满足filter的行数据更新成$newObj(_id自动转对象) + * http://php.net/manual/zh/mongodb-driver-bulkwrite.update.php + * $bulk->update( + * ['x' => 2], + * [['y' => 3]], + * ['multi' => false, 'upsert' => false] + * ); + * + * @param string $namespace + * @param array $filter + * @param array $newObj + * @return bool + * @throws MongoDBException + */ + public function execUpdateColumnId(string $namespace, array $filter = [], array $newObj = []): bool { try { if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { @@ -536,22 +610,71 @@ class MongodbConnection extends Connection implements ConnectionInterface } /** - * 删除数据 + * 删除一条数据 * * @param string $namespace * @param array $filter - * @param bool $limit * @return bool * @throws MongoDBException */ - public function execDelete(string $namespace, array $filter = [], bool $limit = false): bool + public function execDeleteOne(string $namespace, array $filter = []): bool + { + try { + $bulk = new BulkWrite; + $bulk->delete($filter, ['limit' => 1]); + $written = new WriteConcern(WriteConcern::MAJORITY, 1000); + $this->connection->executeBulkWrite($this->config['db'] . '.' . $namespace, $bulk, $written); + $delete = true; + } catch (\Exception $e) { + $delete = false; + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } finally { + $this->pool->release($this); + return $delete; + } + } + + /** + * 删除多条数据 + * + * @param string $namespace + * @param array $filter + * @return bool + * @throws MongoDBException + */ + public function execDeleteMany(string $namespace, array $filter = []): bool + { + try { + $bulk = new BulkWrite; + $bulk->delete($filter, ['limit' => false]); + $written = new WriteConcern(WriteConcern::MAJORITY, 1000); + $this->connection->executeBulkWrite($this->config['db'] . '.' . $namespace, $bulk, $written); + $delete = true; + } catch (\Exception $e) { + $delete = false; + throw new MongoDBException($e->getFile() . $e->getLine() . $e->getMessage()); + } finally { + $this->pool->release($this); + return $delete; + } + } + + /** + * 删除一条数据(_id自动转对象) + * + * @param string $namespace + * @param array $filter + * @return bool + * @throws MongoDBException + */ + public function execDeleteOneId(string $namespace, array $filter = []): bool { try { if (!empty($filter['_id']) && !($filter['_id'] instanceof ObjectId)) { $filter['_id'] = new ObjectId($filter['_id']); } $bulk = new BulkWrite; - $bulk->delete($filter, ['limit' => $limit]); + $bulk->delete($filter, ['limit' => 1]); $written = new WriteConcern(WriteConcern::MAJORITY, 1000); $this->connection->executeBulkWrite($this->config['db'] . '.' . $namespace, $bulk, $written); $delete = true;