Skip to content

Commit 8b9dccb

Browse files
committed
Merge pull request mongodb#1885 from mauri870/drop-compound-index
Fix dropIndex for compound indexes with sorting order
2 parents 9abc84c + d4fc611 commit 8b9dccb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Diff for: src/Jenssegers/Mongodb/Schema/Blueprint.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,18 @@ protected function transformColumns($indexOrColumns)
129129
// Transform the columns to the index name.
130130
$transform = [];
131131

132-
foreach ($indexOrColumns as $column) {
133-
$transform[$column] = $column . '_1';
132+
foreach ($indexOrColumns as $key => $value) {
133+
if (is_int($key)) {
134+
// There is no sorting order, use the default.
135+
$column = $value;
136+
$sorting = '1';
137+
} else {
138+
// This is a column with sorting order e.g 'my_column' => -1.
139+
$column = $key;
140+
$sorting = $value;
141+
}
142+
143+
$transform[$column] = $column . "_" . $sorting;
134144
}
135145

136146
$indexOrColumns = implode('_', $transform);

Diff for: tests/SchemaTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ public function testDropIndex(): void
132132
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
133133
$this->assertFalse($index);
134134

135+
Schema::collection('newcollection', function ($collection) {
136+
$collection->index(['field_a' => -1, 'field_b' => 1]);
137+
});
138+
139+
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
140+
$this->assertNotNull($index);
141+
142+
Schema::collection('newcollection', function ($collection) {
143+
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
144+
});
145+
146+
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
147+
$this->assertFalse($index);
148+
135149
Schema::collection('newcollection', function ($collection) {
136150
$collection->index(['field_a', 'field_b'], 'custom_index_name');
137151
});

0 commit comments

Comments
 (0)