简体   繁体   中英

MongoDB find query with $and $or and $nearSphere in php

After quite some trying out and web research I go crazy with this query. I want to build a query for 'Clubs' around a geo point (distance max 500 meters) in php on MongoDB. But when I run query it ignores the distance limit and shows all clubs in database BUT sorted by distance.

Here is my dataset (2dsphere index geoLoc):

{"_id":ObjectId("547c649e30afe32c23000048"),"name":"Club Ritzz","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"}],"location":{"city":"Mannheim"},"geoLoc":{"type":"Point","coordinates":[8.473665839156,49.484065272756]}}
{"_id":ObjectId("547c649f30afe32c2300004a"),"name":"Das Zimmer Mannheim","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"}],"geoLoc":{"type":"Point","coordinates":[8.4709362941178,49.487260552592]}}
{"_id":ObjectId("547c64ab30afe32c23000063"),"name":"Nationaltheater Mannheim","category":"Arts/entertainment/nightlife","category_list":[{"id":"173883042668223","name":"Theatre"}],"geoLoc":{"type":"Point","coordinates":[8.4776534992592,49.48782606969]}}
{"_id":ObjectId("547c64a130afe32c2300004f"),"name":"SOHO Bar Club Lounge","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"},{"id":"164049010316507","name":"Gastropub"}],"geoLoc":{"type":"Point","coordinates":[8.4630844501277,49.49385193591]}}
{"_id":ObjectId("547c64a730afe32c2300005a"),"name":"Loft Club","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"},{"id":"176139629103647","name":"Dance Club"}],"geoLoc":{"type":"Point","coordinates":[8.4296300196465,49.484211928258]}}

And here my php code (updated Dec-2):

$qry = $pub->find(  
  array( '$and' => 
    array(
      array( 'geoLoc' => 
        array('$nearSphere' =>
          array('$geometry' => 
            array('type'=>'Point',
              'coordinates'=>
              array(
                floatval($sLon), floatval($sLat)
              )
            ),
            'maxDistance' => 500
          )
        )
      ),
      array( '$or' =>
        array(
          array( 'name' => new MongoRegex("/.*club/i")),
          array( 'name' => new MongoRegex("/.*zimm/i"))
        )
      ),
      array('$or' => 
        array(
          array('category_list.name' => 'Night Club'), 
          array('category_list.name' => 'Dance Club'), 
          array('category' => 'Club')
        )
      )
    )
  ),
  array('id' => 1, 'name' => 1, '_id' => 0)
);

Anyone know why the results are not limited to the specified maxDistance ?

I found a similar issue on StackOverflow which outlines that one has to use radians for the maxDistance parameter.

See https://dba.stackexchange.com/questions/23869/nearsphere-returns-too-many-data-what-am-i-missing-am-i-wrong-is-it-a-bug-d

Also it is probably helpful if you'd test the query in mongo shell without using the PHP APIs first (just to see if the query is generally working and append '.explain()' to it to see what generally happens inside DB).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM