Skip to content

Commit ad79fb1

Browse files
authored
Support delete one document with the query builder (#2591)
* Support delete one document MongoDB PHP Library supports deleting one or all documents only. So we cannot accept other limits than null (unlimited) or 1. The notion of limit: 0 meaning "delete all" is an implementation detail of MongoDB's delete command and not a general concept. * Update changelog after repository move
1 parent 9d36d17 commit ad79fb1

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

CHANGELOG.md

+21-20
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,65 @@ All notable changes to this project will be documented in this file.
1919
- Fix validation of unique values when the validated value is found as part of an existing value. [#21](https://github.com/GromNaN/laravel-mongodb/pull/21) by [@GromNaN](https://github.com/GromNaN).
2020
- Support `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb/pull/17) by [@GromNaN](https://github.com/GromNaN).
2121
- Change signature of `Query\Builder::__constructor` to match the parent class [#26](https://github.com/GromNaN/laravel-mongodb-private/pull/26) by [@GromNaN](https://github.com/GromNaN).
22-
- Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2570](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu) and [@GromNaN](https://github.com/GromNaN).
23-
- `Model::unset()` does not persist the change. Call `Model::save()` to persist the change [#2578](https://github.com/jenssegers/laravel-mongodb/pull/2578) by [@GromNaN](https://github.com/GromNaN).
22+
- Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2570](https://github.com/mongodb/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu) and [@GromNaN](https://github.com/GromNaN).
23+
- `Model::unset()` does not persist the change. Call `Model::save()` to persist the change [#2578](https://github.com/mongodb/laravel-mongodb/pull/2578) by [@GromNaN](https://github.com/GromNaN).
24+
- Support delete one document with `Query\Builder::limit(1)->delete()` [#2591](https://github.com/mongodb/laravel-mongodb/pull/2591) by [@GromNaN](https://github.com/GromNaN)
2425

2526
## [3.9.2] - 2022-09-01
2627

2728
### Added
28-
- Add single word name mutators [#2438](https://github.com/jenssegers/laravel-mongodb/pull/2438) by [@RosemaryOrchard](https://github.com/RosemaryOrchard) & [@mrneatly](https://github.com/mrneatly).
29+
- Add single word name mutators [#2438](https://github.com/mongodb/laravel-mongodb/pull/2438) by [@RosemaryOrchard](https://github.com/RosemaryOrchard) & [@mrneatly](https://github.com/mrneatly).
2930

3031
### Fixed
31-
- Fix stringable sort [#2420](https://github.com/jenssegers/laravel-mongodb/pull/2420) by [@apeisa](https://github.com/apeisa).
32+
- Fix stringable sort [#2420](https://github.com/mongodb/laravel-mongodb/pull/2420) by [@apeisa](https://github.com/apeisa).
3233

3334
## [3.9.1] - 2022-03-11
3435

3536
### Added
36-
- Backport support for cursor pagination [#2358](https://github.com/jenssegers/laravel-mongodb/pull/2358) by [@Jeroenwv](https://github.com/Jeroenwv).
37+
- Backport support for cursor pagination [#2358](https://github.com/mongodb/laravel-mongodb/pull/2358) by [@Jeroenwv](https://github.com/Jeroenwv).
3738

3839
### Fixed
39-
- Check if queue service is disabled [#2357](https://github.com/jenssegers/laravel-mongodb/pull/2357) by [@robjbrain](https://github.com/robjbrain).
40+
- Check if queue service is disabled [#2357](https://github.com/mongodb/laravel-mongodb/pull/2357) by [@robjbrain](https://github.com/robjbrain).
4041

4142
## [3.9.0] - 2022-02-17
4243

4344
### Added
44-
- Compatibility with Laravel 9.x [#2344](https://github.com/jenssegers/laravel-mongodb/pull/2344) by [@divine](https://github.com/divine).
45+
- Compatibility with Laravel 9.x [#2344](https://github.com/mongodb/laravel-mongodb/pull/2344) by [@divine](https://github.com/divine).
4546

4647
## [3.8.4] - 2021-05-27
4748

4849
### Fixed
49-
- Fix getRelationQuery breaking changes [#2263](https://github.com/jenssegers/laravel-mongodb/pull/2263) by [@divine](https://github.com/divine).
50-
- Apply fixes produced by php-cs-fixer [#2250](https://github.com/jenssegers/laravel-mongodb/pull/2250) by [@divine](https://github.com/divine).
50+
- Fix getRelationQuery breaking changes [#2263](https://github.com/mongodb/laravel-mongodb/pull/2263) by [@divine](https://github.com/divine).
51+
- Apply fixes produced by php-cs-fixer [#2250](https://github.com/mongodb/laravel-mongodb/pull/2250) by [@divine](https://github.com/divine).
5152

5253
### Changed
53-
- Add doesntExist to passthru [#2194](https://github.com/jenssegers/laravel-mongodb/pull/2194) by [@simonschaufi](https://github.com/simonschaufi).
54-
- Add Model query whereDate support [#2251](https://github.com/jenssegers/laravel-mongodb/pull/2251) by [@yexk](https://github.com/yexk).
55-
- Add transaction free deleteAndRelease() method [#2229](https://github.com/jenssegers/laravel-mongodb/pull/2229) by [@sodoardi](https://github.com/sodoardi).
56-
- Add setDatabase to Jenssegers\Mongodb\Connection [#2236](https://github.com/jenssegers/laravel-mongodb/pull/2236) by [@ThomasWestrelin](https://github.com/ThomasWestrelin).
57-
- Check dates against DateTimeInterface instead of DateTime [#2239](https://github.com/jenssegers/laravel-mongodb/pull/2239) by [@jeromegamez](https://github.com/jeromegamez).
58-
- Move from psr-0 to psr-4 [#2247](https://github.com/jenssegers/laravel-mongodb/pull/2247) by [@divine](https://github.com/divine).
54+
- Add doesntExist to passthru [#2194](https://github.com/mongodb/laravel-mongodb/pull/2194) by [@simonschaufi](https://github.com/simonschaufi).
55+
- Add Model query whereDate support [#2251](https://github.com/mongodb/laravel-mongodb/pull/2251) by [@yexk](https://github.com/yexk).
56+
- Add transaction free deleteAndRelease() method [#2229](https://github.com/mongodb/laravel-mongodb/pull/2229) by [@sodoardi](https://github.com/sodoardi).
57+
- Add setDatabase to Jenssegers\Mongodb\Connection [#2236](https://github.com/mongodb/laravel-mongodb/pull/2236) by [@ThomasWestrelin](https://github.com/ThomasWestrelin).
58+
- Check dates against DateTimeInterface instead of DateTime [#2239](https://github.com/mongodb/laravel-mongodb/pull/2239) by [@jeromegamez](https://github.com/jeromegamez).
59+
- Move from psr-0 to psr-4 [#2247](https://github.com/mongodb/laravel-mongodb/pull/2247) by [@divine](https://github.com/divine).
5960

6061
## [3.8.3] - 2021-02-21
6162

6263
### Changed
63-
- Fix query builder regression [#2204](https://github.com/jenssegers/laravel-mongodb/pull/2204) by [@divine](https://github.com/divine).
64+
- Fix query builder regression [#2204](https://github.com/mongodb/laravel-mongodb/pull/2204) by [@divine](https://github.com/divine).
6465

6566
## [3.8.2] - 2020-12-18
6667

6768
### Changed
68-
- MongodbQueueServiceProvider does not use the DB Facade anymore [#2149](https://github.com/jenssegers/laravel-mongodb/pull/2149) by [@curosmj](https://github.com/curosmj).
69-
- Add escape regex chars to DB Presence Verifier [#1992](https://github.com/jenssegers/laravel-mongodb/pull/1992) by [@andrei-gafton-rtgt](https://github.com/andrei-gafton-rtgt).
69+
- MongodbQueueServiceProvider does not use the DB Facade anymore [#2149](https://github.com/mongodb/laravel-mongodb/pull/2149) by [@curosmj](https://github.com/curosmj).
70+
- Add escape regex chars to DB Presence Verifier [#1992](https://github.com/mongodb/laravel-mongodb/pull/1992) by [@andrei-gafton-rtgt](https://github.com/andrei-gafton-rtgt).
7071

7172
## [3.8.1] - 2020-10-23
7273

7374
### Added
7475
- Laravel 8 support by [@divine](https://github.com/divine).
7576

7677
### Changed
77-
- Fix like with numeric values [#2127](https://github.com/jenssegers/laravel-mongodb/pull/2127) by [@hnassr](https://github.com/hnassr).
78+
- Fix like with numeric values [#2127](https://github.com/mongodb/laravel-mongodb/pull/2127) by [@hnassr](https://github.com/hnassr).
7879

7980
## [3.8.0] - 2020-09-03
8081

8182
### Added
82-
- Laravel 8 support & updated versions of all dependencies [#2108](https://github.com/jenssegers/laravel-mongodb/pull/2108) by [@divine](https://github.com/divine).
83+
- Laravel 8 support & updated versions of all dependencies [#2108](https://github.com/mongodb/laravel-mongodb/pull/2108) by [@divine](https://github.com/divine).

src/Query/Builder.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,14 @@ public function delete($id = null)
703703
$wheres = $this->compileWheres();
704704
$options = $this->inheritConnectionOptions();
705705

706-
$result = $this->collection->deleteMany($wheres, $options);
706+
if (is_int($this->limit)) {
707+
if ($this->limit !== 1) {
708+
throw new \LogicException(sprintf('Delete limit can be 1 or null (unlimited). Got %d', $this->limit));
709+
}
710+
$result = $this->collection->deleteOne($wheres, $options);
711+
} else {
712+
$result = $this->collection->deleteMany($wheres, $options);
713+
}
707714

708715
if (1 == (int) $result->isAcknowledged()) {
709716
return $result->getDeletedCount();

tests/QueryTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,40 @@ public function testMultipleSortOrder(): void
548548
$this->assertEquals('John Doe', $subset[1]->name);
549549
$this->assertEquals('Brett Boe', $subset[2]->name);
550550
}
551+
552+
public function testDelete(): void
553+
{
554+
// Check fixtures
555+
$this->assertEquals(3, User::where('title', 'admin')->count());
556+
557+
// Delete a single document with filter
558+
User::where('title', 'admin')->limit(1)->delete();
559+
$this->assertEquals(2, User::where('title', 'admin')->count());
560+
561+
// Delete all with filter
562+
User::where('title', 'admin')->delete();
563+
$this->assertEquals(0, User::where('title', 'admin')->count());
564+
565+
// Check remaining fixtures
566+
$this->assertEquals(6, User::count());
567+
568+
// Delete a single document
569+
User::limit(1)->delete();
570+
$this->assertEquals(5, User::count());
571+
572+
// Delete all
573+
User::limit(null)->delete();
574+
$this->assertEquals(0, User::count());
575+
}
576+
577+
/**
578+
* @testWith [0]
579+
* [2]
580+
*/
581+
public function testDeleteException(int $limit): void
582+
{
583+
$this->expectException(\LogicException::class);
584+
$this->expectExceptionMessage('Delete limit can be 1 or null (unlimited).');
585+
User::limit($limit)->delete();
586+
}
551587
}

0 commit comments

Comments
 (0)