Skip to content

Commit d41805d

Browse files
committed
Update MongoDB Index
1 parent 97ec884 commit d41805d

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

src/Database/Drivers/MongoDB/Index.php

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,53 +62,9 @@ public static function setIndexes(Collection $collection, $indexes)
6262
continue;
6363
}
6464

65-
switch ($type) {
66-
67-
case 'TEXT':
68-
$indexType = 'text';
69-
break;
70-
case 'INDEX':
71-
$indexType = 1;
72-
break;
73-
case 'UNIQUE':
74-
$indexType = 1;
75-
$options['unique'] = true;
76-
break;
77-
case 'UNIQUE_DESC':
78-
$indexType = -1;
79-
$options['unique'] = true;
80-
break;
81-
case 'TTL':
82-
$indexType = 1;
83-
$options['expireAfterSeconds'] = 3600;
84-
break;
85-
case 'SPARSE':
86-
$indexType = 1;
87-
$options['sparse'] = true;
88-
break;
89-
case 'SPARSE_DESC':
90-
$indexType = -1;
91-
$options['sparse'] = true;
92-
break;
93-
case 'SPARSE_UNIQUE':
94-
$indexType = 1;
95-
$options['sparse'] = true;
96-
$options['unique'] = true;
97-
case 'SPARSE_UNIQUE_DESC':
98-
$indexType = -1;
99-
$options['sparse'] = true;
100-
$options['unique'] = true;
101-
break;
102-
case 'ASC':
103-
$indexType = 1;
104-
break;
105-
case 'DESC':
106-
$indexType = -1;
107-
break;
108-
default:
109-
$indexType = 1;
110-
break;
111-
}
65+
$indexDetails = static::getIndexDetails($type);
66+
$indexType = $indexDetails['type'];
67+
$options = $indexDetails['options'];
11268

11369
$options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type);
11470

@@ -120,4 +76,57 @@ public static function setIndexes(Collection $collection, $indexes)
12076

12177
return true;
12278
}
79+
80+
public static function getIndexDetails($type)
81+
{
82+
$indexType = 1;
83+
$options = [];
84+
85+
switch ($type) {
86+
87+
case 'TEXT':
88+
$indexType = 'text';
89+
break;
90+
case 'INDEX':
91+
$indexType = 1;
92+
break;
93+
case 'UNIQUE':
94+
$indexType = 1;
95+
$options['unique'] = true;
96+
break;
97+
case 'UNIQUE_DESC':
98+
$indexType = -1;
99+
$options['unique'] = true;
100+
break;
101+
case 'TTL':
102+
$indexType = 1;
103+
$options['expireAfterSeconds'] = 3600;
104+
break;
105+
case 'SPARSE':
106+
$indexType = 1;
107+
$options['sparse'] = true;
108+
break;
109+
case 'SPARSE_DESC':
110+
$indexType = -1;
111+
$options['sparse'] = true;
112+
break;
113+
case 'SPARSE_UNIQUE':
114+
$indexType = 1;
115+
$options['sparse'] = true;
116+
$options['unique'] = true;
117+
case 'SPARSE_UNIQUE_DESC':
118+
$indexType = -1;
119+
$options['sparse'] = true;
120+
$options['unique'] = true;
121+
break;
122+
case 'ASC':
123+
$indexType = 1;
124+
break;
125+
case 'DESC':
126+
$indexType = -1;
127+
break;
128+
}
129+
130+
return ["type" => $indexType, "options" => $options];
131+
}
123132
}

0 commit comments

Comments
 (0)