@@ -181,7 +181,7 @@ private function catchMongoException(\Throwable $e)
181181 * @return array
182182 * @throws MongoDBException
183183 */
184- public function executeFindOne (string $ namespace , array $ filter = [], array $ options = [])
184+ public function execFindOne (string $ namespace , array $ filter = [], array $ options = [])
185185 {
186186 // 查询数据
187187 $ result = [];
@@ -212,7 +212,7 @@ public function executeFindOne(string $namespace, array $filter = [], array $opt
212212 * @return array
213213 * @throws MongoDBException
214214 */
215- public function executeFindAll (string $ namespace , array $ filter = [], array $ options = [])
215+ public function execFindAll (string $ namespace , array $ filter = [], array $ options = [])
216216 {
217217 // 查询数据
218218 $ result = [];
@@ -286,7 +286,7 @@ public function execFindPagination(string $namespace, int $limit = 10, int $curr
286286 * @return array
287287 * @throws MongoDBException
288288 */
289- public function executeFetchOne (string $ namespace , array $ filter = [], array $ options = [])
289+ public function execFindOneId (string $ namespace , array $ filter = [], array $ options = [])
290290 {
291291 if (!empty ($ filter ['_id ' ]) && !($ filter ['_id ' ] instanceof ObjectId)) {
292292 $ filter ['_id ' ] = new ObjectId ($ filter ['_id ' ]);
@@ -322,7 +322,7 @@ public function executeFetchOne(string $namespace, array $filter = [], array $op
322322 * @return array
323323 * @throws MongoDBException
324324 */
325- public function executeFetchAll (string $ namespace , array $ filter = [], array $ options = [])
325+ public function execFindAllId (string $ namespace , array $ filter = [], array $ options = [])
326326 {
327327 if (!empty ($ filter ['_id ' ]) && !($ filter ['_id ' ] instanceof ObjectId)) {
328328 $ filter ['_id ' ] = new ObjectId ($ filter ['_id ' ]);
@@ -358,7 +358,7 @@ public function executeFetchAll(string $namespace, array $filter = [], array $op
358358 * @return array
359359 * @throws MongoDBException
360360 */
361- public function execFetchPagination (string $ namespace , int $ limit = 10 , int $ currentPage = 0 , array $ filter = [], array $ options = [])
361+ public function execFindPaginationId (string $ namespace , int $ limit = 10 , int $ currentPage = 0 , array $ filter = [], array $ options = [])
362362 {
363363 if (!empty ($ filter ['_id ' ]) && !($ filter ['_id ' ] instanceof ObjectId)) {
364364 $ filter ['_id ' ] = new ObjectId ($ filter ['_id ' ]);
@@ -437,7 +437,7 @@ public function execInsertOne(string $namespace, array $data = [])
437437 * @return bool|string
438438 * @throws MongoDBException
439439 */
440- public function execInsertAll (string $ namespace , array $ data = [])
440+ public function execInsertMany (string $ namespace , array $ data = [])
441441 {
442442 try {
443443 $ bulk = new BulkWrite ();
@@ -471,6 +471,80 @@ public function execInsertAll(string $namespace, array $data = [])
471471 * @throws MongoDBException
472472 */
473473 public function execUpdateRow (string $ namespace , array $ filter = [], array $ newObj = []): bool
474+ {
475+ try {
476+ $ bulk = new BulkWrite ;
477+ $ bulk ->update (
478+ $ filter ,
479+ ['$set ' => $ newObj ],
480+ ['multi ' => true , 'upsert ' => false ]
481+ );
482+ $ written = new WriteConcern (WriteConcern::MAJORITY , 1000 );
483+ $ result = $ this ->connection ->executeBulkWrite ($ this ->config ['db ' ] . '. ' . $ namespace , $ bulk , $ written );
484+ $ modifiedCount = $ result ->getModifiedCount ();
485+ $ update = $ modifiedCount == 0 ? false : true ;
486+ } catch (\Exception $ e ) {
487+ $ update = false ;
488+ throw new MongoDBException ($ e ->getFile () . $ e ->getLine () . $ e ->getMessage ());
489+ } finally {
490+ $ this ->pool ->release ($ this );
491+ return $ update ;
492+ }
493+ }
494+
495+ /**
496+ * 数据更新, 效果是满足filter的行数据更新成$newObj
497+ * http://php.net/manual/zh/mongodb-driver-bulkwrite.update.php
498+ * $bulk->update(
499+ * ['x' => 2],
500+ * [['y' => 3]],
501+ * ['multi' => false, 'upsert' => false]
502+ * );
503+ *
504+ * @param string $namespace
505+ * @param array $filter
506+ * @param array $newObj
507+ * @return bool
508+ * @throws MongoDBException
509+ */
510+ public function execUpdateColumn (string $ namespace , array $ filter = [], array $ newObj = []): bool
511+ {
512+ try {
513+ $ bulk = new BulkWrite ;
514+ $ bulk ->update (
515+ $ filter ,
516+ ['$set ' => $ newObj ],
517+ ['multi ' => false , 'upsert ' => false ]
518+ );
519+ $ written = new WriteConcern (WriteConcern::MAJORITY , 1000 );
520+ $ result = $ this ->connection ->executeBulkWrite ($ this ->config ['db ' ] . '. ' . $ namespace , $ bulk , $ written );
521+ $ modifiedCount = $ result ->getModifiedCount ();
522+ $ update = $ modifiedCount == 1 ? true : false ;
523+ } catch (\Exception $ e ) {
524+ $ update = false ;
525+ throw new MongoDBException ($ e ->getFile () . $ e ->getLine () . $ e ->getMessage ());
526+ } finally {
527+ $ this ->release ();
528+ return $ update ;
529+ }
530+ }
531+
532+ /**
533+ * 数据更新,效果是满足filter的行,只更新$newObj中的$set出现的字段(_id自动转对象)
534+ * http://php.net/manual/zh/mongodb-driver-bulkwrite.update.php
535+ * $bulk->update(
536+ * ['x' => 2],
537+ * ['$set' => ['y' => 3]],
538+ * ['multi' => false, 'upsert' => false]
539+ * );
540+ *
541+ * @param string $namespace
542+ * @param array $filter
543+ * @param array $newObj
544+ * @return bool
545+ * @throws MongoDBException
546+ */
547+ public function execUpdateRowId (string $ namespace , array $ filter = [], array $ newObj = []): bool
474548 {
475549 try {
476550 if (!empty ($ filter ['_id ' ]) && !($ filter ['_id ' ] instanceof ObjectId)) {
@@ -496,7 +570,7 @@ public function execUpdateRow(string $namespace, array $filter = [], array $newO
496570 }
497571
498572 /**
499- * 数据更新, 效果是满足filter的行数据更新成$newObj
573+ * 数据更新, 效果是满足filter的行数据更新成$newObj(_id自动转对象)
500574 * http://php.net/manual/zh/mongodb-driver-bulkwrite.update.php
501575 * $bulk->update(
502576 * ['x' => 2],
@@ -510,7 +584,7 @@ public function execUpdateRow(string $namespace, array $filter = [], array $newO
510584 * @return bool
511585 * @throws MongoDBException
512586 */
513- public function execUpdateColumn (string $ namespace , array $ filter = [], array $ newObj = []): bool
587+ public function execUpdateColumnId (string $ namespace , array $ filter = [], array $ newObj = []): bool
514588 {
515589 try {
516590 if (!empty ($ filter ['_id ' ]) && !($ filter ['_id ' ] instanceof ObjectId)) {
@@ -536,22 +610,71 @@ public function execUpdateColumn(string $namespace, array $filter = [], array $n
536610 }
537611
538612 /**
539- * 删除数据
613+ * 删除一条数据
614+ *
615+ * @param string $namespace
616+ * @param array $filter
617+ * @return bool
618+ * @throws MongoDBException
619+ */
620+ public function execDeleteOne (string $ namespace , array $ filter = []): bool
621+ {
622+ try {
623+ $ bulk = new BulkWrite ;
624+ $ bulk ->delete ($ filter , ['limit ' => 1 ]);
625+ $ written = new WriteConcern (WriteConcern::MAJORITY , 1000 );
626+ $ this ->connection ->executeBulkWrite ($ this ->config ['db ' ] . '. ' . $ namespace , $ bulk , $ written );
627+ $ delete = true ;
628+ } catch (\Exception $ e ) {
629+ $ delete = false ;
630+ throw new MongoDBException ($ e ->getFile () . $ e ->getLine () . $ e ->getMessage ());
631+ } finally {
632+ $ this ->pool ->release ($ this );
633+ return $ delete ;
634+ }
635+ }
636+
637+ /**
638+ * 删除多条数据
639+ *
640+ * @param string $namespace
641+ * @param array $filter
642+ * @return bool
643+ * @throws MongoDBException
644+ */
645+ public function execDeleteMany (string $ namespace , array $ filter = []): bool
646+ {
647+ try {
648+ $ bulk = new BulkWrite ;
649+ $ bulk ->delete ($ filter , ['limit ' => false ]);
650+ $ written = new WriteConcern (WriteConcern::MAJORITY , 1000 );
651+ $ this ->connection ->executeBulkWrite ($ this ->config ['db ' ] . '. ' . $ namespace , $ bulk , $ written );
652+ $ delete = true ;
653+ } catch (\Exception $ e ) {
654+ $ delete = false ;
655+ throw new MongoDBException ($ e ->getFile () . $ e ->getLine () . $ e ->getMessage ());
656+ } finally {
657+ $ this ->pool ->release ($ this );
658+ return $ delete ;
659+ }
660+ }
661+
662+ /**
663+ * 删除一条数据(_id自动转对象)
540664 *
541665 * @param string $namespace
542666 * @param array $filter
543- * @param bool $limit
544667 * @return bool
545668 * @throws MongoDBException
546669 */
547- public function execDelete (string $ namespace , array $ filter = [], bool $ limit = false ): bool
670+ public function execDeleteOneId (string $ namespace , array $ filter = []): bool
548671 {
549672 try {
550673 if (!empty ($ filter ['_id ' ]) && !($ filter ['_id ' ] instanceof ObjectId)) {
551674 $ filter ['_id ' ] = new ObjectId ($ filter ['_id ' ]);
552675 }
553676 $ bulk = new BulkWrite ;
554- $ bulk ->delete ($ filter , ['limit ' => $ limit ]);
677+ $ bulk ->delete ($ filter , ['limit ' => 1 ]);
555678 $ written = new WriteConcern (WriteConcern::MAJORITY , 1000 );
556679 $ this ->connection ->executeBulkWrite ($ this ->config ['db ' ] . '. ' . $ namespace , $ bulk , $ written );
557680 $ delete = true ;
0 commit comments