File tree 2 files changed +26
-2
lines changed
src/Jenssegers/Mongodb/Schema
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)
129
129
// Transform the columns to the index name.
130
130
$ transform = [];
131
131
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 ;
134
144
}
135
145
136
146
$ indexOrColumns = implode ('_ ' , $ transform );
Original file line number Diff line number Diff line change @@ -132,6 +132,20 @@ public function testDropIndex(): void
132
132
$ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
133
133
$ this ->assertFalse ($ index );
134
134
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
+
135
149
Schema::collection ('newcollection ' , function ($ collection ) {
136
150
$ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
137
151
});
You can’t perform that action at this time.
0 commit comments