Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit ad43a96

Browse files
committed
Fix nested $geometry
1 parent c1c2d08 commit ad43a96

File tree

7 files changed

+33
-53
lines changed

7 files changed

+33
-53
lines changed

generator/config/query/near.yaml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: $near
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/near/'
44
type:
55
- fieldQuery
6-
encode: object
6+
encode: dollar_object
77
description: |
88
Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
99
arguments:
@@ -41,15 +41,3 @@ tests:
4141
- 40.78
4242
$minDistance: 1000
4343
$maxDistance: 5000
44-
45-
-
46-
name: 'Query on Legacy Coordinates'
47-
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/near/#query-on-legacy-coordinates'
48-
pipeline:
49-
-
50-
$match:
51-
location:
52-
$near:
53-
- -73.9667
54-
- 40.78
55-
$maxDistance: 0.1

generator/config/query/nearSphere.yaml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: $nearSphere
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere/'
44
type:
55
- fieldQuery
6-
encode: object
6+
encode: dollar_object
77
description: |
88
Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.
99
arguments:
@@ -36,19 +36,6 @@ tests:
3636
$nearSphere:
3737
$geometry:
3838
type: 'Point'
39-
coordinates:
40-
- -73.9667
41-
- 40.78
39+
coordinates: [-73.9667, 40.78]
4240
$minDistance: 1000
4341
$maxDistance: 5000
44-
-
45-
name: 'Specify Center Point Using Legacy Coordinates'
46-
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere/#specify-center-point-using-legacy-coordinates'
47-
pipeline:
48-
-
49-
$match:
50-
location:
51-
$nearSphere:
52-
- -73.9667
53-
- 40.78
54-
$maxDistance: 0.1

src/Builder/BuilderEncoder.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use MongoDB\Exception\UnsupportedValueException;
2727
use stdClass;
2828

29+
use function array_key_exists;
2930
use function array_key_first;
3031
use function assert;
3132
use function get_debug_type;
@@ -182,7 +183,19 @@ private function encodeAsDollarObject(OperatorInterface $value): stdClass
182183
continue;
183184
}
184185

185-
$result->{'$' . $key} = $this->recursiveEncode($val);
186+
$val = $this->recursiveEncode($val);
187+
188+
if ($key === 'geometry') {
189+
if (is_object($val) && property_exists($val, '$geometry')) {
190+
$result->{'$geometry'} = $val->{'$geometry'};
191+
} elseif (is_array($val) && array_key_exists('$geometry', $val)) {
192+
$result->{'$geometry'} = $val->{'$geometry'};
193+
} else {
194+
$result->{'$geometry'} = $val;
195+
}
196+
} else {
197+
$result->{'$' . $key} = $val;
198+
}
186199
}
187200

188201
return $this->wrap($value, $result);

src/Builder/Query/NearOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Query/NearSphereOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/Query/NearOperatorTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,4 @@ public function testQueryOnGeoJSONData(): void
3131

3232
$this->assertSamePipeline(Pipelines::NearQueryOnGeoJSONData, $pipeline);
3333
}
34-
35-
public function testQueryOnLegacyCoordinates(): void
36-
{
37-
$pipeline = new Pipeline(
38-
Stage::match(
39-
location: Query::near(
40-
[-73.9667, 40.78],
41-
maxDistance: 0.10,
42-
),
43-
),
44-
);
45-
46-
$this->assertSamePipeline(Pipelines::NearQueryOnLegacyCoordinates, $pipeline);
47-
}
4834
}

tests/Builder/Query/NearSphereOperatorTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace MongoDB\Tests\Builder\Query;
66

77
use MongoDB\Builder\Pipeline;
8+
use MongoDB\Builder\Query;
9+
use MongoDB\Builder\Stage;
810
use MongoDB\Tests\Builder\PipelineTestCase;
911

1012
/**
@@ -14,15 +16,19 @@ class NearSphereOperatorTest extends PipelineTestCase
1416
{
1517
public function testSpecifyCenterPointUsingGeoJSON(): void
1618
{
17-
$pipeline = new Pipeline();
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
location: Query::nearSphere(
22+
Query::geometry(
23+
type: 'Point',
24+
coordinates: [-73.9667, 40.78],
25+
),
26+
minDistance: 1000,
27+
maxDistance: 5000,
28+
),
29+
),
30+
);
1831

1932
$this->assertSamePipeline(Pipelines::NearSphereSpecifyCenterPointUsingGeoJSON, $pipeline);
2033
}
21-
22-
public function testSpecifyCenterPointUsingLegacyCoordinates(): void
23-
{
24-
$pipeline = new Pipeline();
25-
26-
$this->assertSamePipeline(Pipelines::NearSphereSpecifyCenterPointUsingLegacyCoordinates, $pipeline);
27-
}
2834
}

0 commit comments

Comments
 (0)