Skip to content

Commit 828982f

Browse files
authored
Merge pull request mongodb#1993 from Yahatix/master
[3.x] fix truncate to delete items in collection
2 parents 08f4995 + 2223548 commit 828982f

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

Diff for: src/Jenssegers/Mongodb/Query/Builder.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Support\LazyCollection;
1212
use Illuminate\Support\Str;
1313
use Jenssegers\Mongodb\Connection;
14-
use MongoCollection;
1514
use MongoDB\BSON\Binary;
1615
use MongoDB\BSON\ObjectID;
1716
use MongoDB\BSON\Regex;
@@ -26,7 +25,7 @@ class Builder extends BaseBuilder
2625
{
2726
/**
2827
* The database collection.
29-
* @var MongoCollection
28+
* @var \MongoDB\Collection
3029
*/
3130
protected $collection;
3231

@@ -725,15 +724,11 @@ public function from($collection, $as = null)
725724
/**
726725
* @inheritdoc
727726
*/
728-
public function truncate()
727+
public function truncate(): bool
729728
{
730-
$options = [
731-
'typeMap' => ['root' => 'object', 'document' => 'object'],
732-
];
733-
734-
$result = $this->collection->drop($options);
729+
$result = $this->collection->deleteMany([]);
735730

736-
return (1 == (int) $result->ok);
731+
return (1 === (int) $result->isAcknowledged());
737732
}
738733

739734
/**

Diff for: tests/QueryBuilderTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ public function testDelete()
176176
public function testTruncate()
177177
{
178178
DB::collection('users')->insert(['name' => 'John Doe']);
179+
DB::collection('users')->insert(['name' => 'John Doe']);
180+
$this->assertEquals(2, DB::collection('users')->count());
179181
$result = DB::collection('users')->truncate();
180-
$this->assertEquals(1, $result);
182+
$this->assertTrue($result);
181183
$this->assertEquals(0, DB::collection('users')->count());
182184
}
183185

0 commit comments

Comments
 (0)