MongoDB在php中查找带$和$或$ nearSphere的查询

时间:2014-12-01 15:07:57

标签: php mongodb mongodb-query

经过一番尝试和网络研究后,我对这个问题感到疯狂。我想在MongoDB上的php中围绕地理点(距离最远500米)构建一个“俱乐部”查询。 但是当我运行查询时,它会忽略距离限制,并按距离显示数据库中的所有球杆。

这是我的数据集(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]}}

这里我的php代码(12月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)
);

任何人都知道为什么结果不限于指定的maxDistance

1 个答案:

答案 0 :(得分:0)

我在StackOverflow上发现了一个类似的问题,它概述了必须使用radians作为maxDistance参数。

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

如果你在没有首先使用PHP API的情况下测试mongo shell中的查询(也就是查看查询是否正常工作并附加' .explain()'它可以看到DB内部通常会发生什么。