繁体   English   中英

MongoDB在PHP中使用$ and $ or和$ nearSphere查找查询

[英]MongoDB find query with $and $or and $nearSphere in php

经过大量的尝试和网络研究之后,我对该查询发了疯。 我想在MongoDB上的php中围绕一个地理点(最大距离为500米)构建“俱乐部”查询。 但是,当我运行查询时,它忽略了距离限制,并显示了数据库中所有按距离排序的球杆。

这是我的数据集(2dsphere索引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吗?

我在StackOverflow上发现了类似的问题,该问题概述了必须对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内部通常发生的情况)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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