File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
src/Jenssegers/Mongodb/Schema Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff 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 );
Original file line number Diff line number Diff 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 });
You can’t perform that action at this time.
0 commit comments