@@ -297,10 +297,9 @@ public function toMql(): array
297
297
$ columns = [];
298
298
}
299
299
300
- $ wheres = $ this ->compileWheres ();
301
-
302
300
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
303
301
if ($ this ->groups || $ this ->aggregate ) {
302
+ $ wheres = $ this ->compileWheres (aggregation: true );
304
303
$ group = [];
305
304
$ unwinds = [];
306
305
@@ -404,6 +403,8 @@ public function toMql(): array
404
403
return ['aggregate ' => [$ pipeline , $ options ]];
405
404
}
406
405
406
+ $ wheres = $ this ->compileWheres (aggregation: false );
407
+
407
408
// Distinct query
408
409
if ($ this ->distinct ) {
409
410
// Return distinct results directly
@@ -414,6 +415,7 @@ public function toMql(): array
414
415
return ['distinct ' => [$ column , $ wheres , $ options ]];
415
416
}
416
417
418
+
417
419
// Normal query
418
420
// Convert select columns to simple projections.
419
421
$ projection = array_fill_keys ($ columns , true );
@@ -1133,7 +1135,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
1133
1135
*
1134
1136
* @return array
1135
1137
*/
1136
- protected function compileWheres (): array
1138
+ protected function compileWheres (bool $ aggregation ): array
1137
1139
{
1138
1140
// The wheres to compile.
1139
1141
$ wheres = $ this ->wheres ?: [];
@@ -1150,6 +1152,17 @@ protected function compileWheres(): array
1150
1152
if (isset ($ this ->conversion [$ where ['operator ' ]])) {
1151
1153
$ where ['operator ' ] = $ this ->conversion [$ where ['operator ' ]];
1152
1154
}
1155
+
1156
+ if ($ where ['operator ' ] === 'near ' && isset ($ where ['value ' ]['$geometry ' ]) && isset ($ where ['value ' ]['$maxDistance ' ])) {
1157
+ $ where ['operator ' ] = 'geoWithin ' ;
1158
+ $ where ['value ' ] = [
1159
+ '$centerSphere ' => [
1160
+ $ where ['value ' ]['$geometry ' ]['coordinates ' ],
1161
+ // Convert meters to radians
1162
+ $ where ['value ' ]['$maxDistance ' ] / 6378100 ,
1163
+ ],
1164
+ ];
1165
+ }
1153
1166
}
1154
1167
1155
1168
// Convert column name to string to use as array key
0 commit comments