Skip to content

Commit fb03b61

Browse files
committed
Convert $near into $geoWithin for count
1 parent 046b92a commit fb03b61

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
66
* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)
77
* Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062)
88
* Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064)
9+
* Convert `$near` to `$geoWithin` in queries using aggregation by @GromNaN in [#3073](https://github.com/mongodb/laravel-mongodb/pull/3073)
910

1011
## [4.7.0] - 2024-07-19
1112

src/Query/Builder.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,9 @@ public function toMql(): array
297297
$columns = [];
298298
}
299299

300-
$wheres = $this->compileWheres();
301-
302300
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
303301
if ($this->groups || $this->aggregate) {
302+
$wheres = $this->compileWheres(true);
304303
$group = [];
305304
$unwinds = [];
306305

@@ -404,6 +403,8 @@ public function toMql(): array
404403
return ['aggregate' => [$pipeline, $options]];
405404
}
406405

406+
$wheres = $this->compileWheres();
407+
407408
// Distinct query
408409
if ($this->distinct) {
409410
// Return distinct results directly
@@ -1133,7 +1134,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
11331134
*
11341135
* @return array
11351136
*/
1136-
protected function compileWheres(): array
1137+
protected function compileWheres(bool $aggregation = false): array
11371138
{
11381139
// The wheres to compile.
11391140
$wheres = $this->wheres ?: [];
@@ -1150,6 +1151,18 @@ protected function compileWheres(): array
11501151
if (isset($this->conversion[$where['operator']])) {
11511152
$where['operator'] = $this->conversion[$where['operator']];
11521153
}
1154+
1155+
// Convert $near to $geoWithin for aggregations
1156+
if ($aggregation && $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+
}
11531166
}
11541167

11551168
// Convert column name to string to use as array key

tests/QueryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,27 @@ public function testPaginateDistinct(): void
574574
User::distinct('age')->paginate(2);
575575
}
576576

577+
public function testPaginateNear(): void
578+
{
579+
User::insert([
580+
['name' => 'Store A', 'position' => [9.224596977233887, 52.03082275390625]],
581+
['name' => 'Store B', 'position' => [9.224596977233887, 52.03082275390625]],
582+
['name' => 'Store C', 'position' => [9.3731451034548, 52.10194]],
583+
]);
584+
585+
$query = User::where('position', 'near', [
586+
'$geometry' => [
587+
'type' => 'Point',
588+
'coordinates' => [9.3731451034546, 52.1019308],
589+
],
590+
'$maxDistance' => 50,
591+
]);
592+
$result = $query->paginate();
593+
594+
$this->assertCount(1, $result->items());
595+
$this->assertSame('Store C', $result->first()->name);
596+
}
597+
577598
public function testUpdate(): void
578599
{
579600
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));

0 commit comments

Comments
 (0)