Skip to content

Commit 040c1fa

Browse files
committed
Make the truncate function only remove documents and not drop the collection
Resolves: mongodb#1306
1 parent 279b18c commit 040c1fa

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Builder extends BaseBuilder
2020
{
2121
/**
2222
* The database collection.
23-
* @var MongoCollection
23+
* @var \MongoDB\Collection
2424
*/
2525
protected $collection;
2626

@@ -697,16 +697,13 @@ public function from($collection, $as = null)
697697

698698
/**
699699
* @inheritdoc
700+
* @return boolean
700701
*/
701-
public function truncate()
702+
public function truncate(): bool
702703
{
703-
$options = [
704-
'typeMap' => ['root' => 'object', 'document' => 'object'],
705-
];
706-
707-
$result = $this->collection->drop($options);
704+
$result = $this->collection->deleteMany([]);
708705

709-
return (1 == (int) $result->ok);
706+
return (1 === (int)$result->isAcknowledged());
710707
}
711708

712709
/**

tests/QueryBuilderTest.php

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

0 commit comments

Comments
 (0)