Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional parameters to nearSphere and near GeoFuntions #2545

Closed
AlexMinaev19 opened this issue Aug 8, 2023 · 2 comments · Fixed by #2583
Closed

Add optional parameters to nearSphere and near GeoFuntions #2545

AlexMinaev19 opened this issue Aug 8, 2023 · 2 comments · Fixed by #2583
Milestone

Comments

@AlexMinaev19
Copy link

AlexMinaev19 commented Aug 8, 2023

Feature Request

Currently, mongodb-odm supports only the required parameters of nearSphere and near GeoFuntions: GeoJson point or legacy coordinate point.
For example, nearSphere function (same for near):

$this->createQueryBuilder()
    ->field('location')
    ->nearSphere(GeoJson\Geometry\Point(<longitude>, <latitude>))
$this->createQueryBuilder()
    ->field('location')
    ->nearSphere([
        'type' => 'Point',
        'coordinates' => [<longitude>, <latitude>]
    ])

or

$this->createQueryBuilder()
    ->field('location')
    ->nearSphere(<longitude>, <latitude>)

According to the latest documentation of nearSphere operator and near operator, in addition, to the required parameters, you can also use optional $maxDistance and $minDistance. They needed to limit the result set of operators, but currently mongodb-odm doesn't support them.

Q A
New Feature yes
RFC no
BC Break no

Summary

I suggest the next:

  1. Add optional parameters to Builder::nearSphere() and Builder::near() methods:
- public function nearSphere($x, $y = null): self
+ public function nearSphere($x, $y = null, $maxDistance = null, $minDistance = null): self
{
-    $this->expr->nearSphere($x, $y);
+    $this->expr->nearSphere($x, $y, $maxDistance, $minDistance);
     
     return $this;
}
  1. Add optional parameters to Exrp::nearSphere and Expr::near methods:
- public function nearSphere($x, $y = null): self
+ public function nearSphere($x, $y = null, $maxDistance = null, $minDistance = null): self 
{
-     if ($x instanceof Point) {
-         $x = $x->jsonSerialize();
-     }

-     if (is_array($x)) {
-         return $this->operator('$nearSphere', ['$geometry' => $x]);
-     }

-     return $this->operator('$nearSphere', [$x, $y]);

+     $optional = [];

+     if ($x instanceof Point) {
+         $x = $x->jsonSerialize();
+     }

+     if ($maxDistance !== null) {
+         $optional += ['$maxDistance' => $maxDistance];
+     }

+     if ($minDistance !== null) {
+         $optional += ['$minDistance' => $minDistance];
+     }

+     if (is_array($x)) {
+         return $this->operator('$nearSphere', ['$geometry' => $x] + $optional);
+     }
        
+     $expr = $this->operator('$nearSphere', [$x, $y]);
        
+     if ($maxDistance !== null) {
+         $expr = $expr->operator('$maxDistance', $maxDistance);
+     }

+     if ($minDistance !== null) {
+         $expr = $expr->operator('$minDistance', $minDistance);
+     }

+     return  $expr;
}

It will allow us to use optional parameters and filter result set better.

@malarzm
Copy link
Member

malarzm commented Nov 4, 2023

@alcaeus any thoughts on this one? :)

@alcaeus
Copy link
Member

alcaeus commented Nov 23, 2023

See #2583

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants