Skip to content

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

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

Closed
sharathm89 opened this issue Jul 5, 2016 · 1 comment

Comments

@sharathm89
Copy link

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'}
  }
);
@RafaelPlantard
Copy link

'property_name' => [
	'$nearSphere' => [
		'$geometry'    => [
			'type'        => 'Point',
			'coordinates' => [
				$latitude,
				$longitude
			]
		],
		'$maxDistance' => $rangeInKm * self::meters_per_km
	]
]

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

No branches or pull requests

3 participants