Skip to content

Commit

Permalink
Merge pull request #1993 from Yahatix/master
Browse files Browse the repository at this point in the history
[3.x] fix truncate to delete items in collection
  • Loading branch information
Smolevich committed Jul 22, 2020
2 parents 08f4995 + 2223548 commit 828982f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 4 additions & 9 deletions src/Jenssegers/Mongodb/Query/Builder.php
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Connection;
use MongoCollection;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
Expand All @@ -26,7 +25,7 @@ class Builder extends BaseBuilder
{
/**
* The database collection.
* @var MongoCollection
* @var \MongoDB\Collection
*/
protected $collection;

Expand Down Expand Up @@ -725,15 +724,11 @@ public function from($collection, $as = null)
/**
* @inheritdoc
*/
public function truncate()
public function truncate(): bool
{
$options = [
'typeMap' => ['root' => 'object', 'document' => 'object'],
];

$result = $this->collection->drop($options);
$result = $this->collection->deleteMany([]);

return (1 == (int) $result->ok);
return (1 === (int) $result->isAcknowledged());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/QueryBuilderTest.php
Expand Up @@ -176,8 +176,10 @@ public function testDelete()
public function testTruncate()
{
DB::collection('users')->insert(['name' => 'John Doe']);
DB::collection('users')->insert(['name' => 'John Doe']);
$this->assertEquals(2, DB::collection('users')->count());
$result = DB::collection('users')->truncate();
$this->assertEquals(1, $result);
$this->assertTrue($result);
$this->assertEquals(0, DB::collection('users')->count());
}

Expand Down

0 comments on commit 828982f

Please sign in to comment.