Skip to content

Use where condition in Laravel MongoDB along with GeoSpatial whereRaw function #886

Closed
@sharathm89

Description

@sharathm89

I am able to search for records using Geospatial query but how to add a where condition for filtering the records in laravel mongodb.

Below is my code to fetch the records without where condition.

$whereObj = ['loc' => ['$near' => ['$geometry' => 
    ['type' => "Point",'coordinates' => [$data['lng'],$data['lat']]], 
    '$maxDistance' => 8000]]];

$list = Places::whereRaw($whereObj)->get();

I tried adding a query object in the above $whereObj but i don't get any results when its executed.
Below is the code with where condition to filter data.


$whereObj = ['loc' => ['$near' => ['$geometry' => 
        ['type' => "Point",'coordinates' => [$data['lng'],$data['lat']]], 
        '$maxDistance' => 8000]], 'query' => ['status' => 'ACTIVE']];

$list = Places::whereRaw($whereObj)->get();

If I run the below query in mongo shell it works.

db.Places.find(
  {
    loc : {
      $near : {
        $geometry : { 
          type : "Point" , 
          coordinates : [77.612865, 12.970787]  
        }, 
        $maxDistance : 2000
      }
    },
    query : {status : 'ACTIVE'}
  }
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions