【问题标题】:MongoDB find query with $and $or and $nearSphere in phpMongoDB 在 php 中使用 $and $or 和 $nearSphere 查找查询
【发布时间】:2014-12-01 15:07:56
【问题描述】:

经过相当多的尝试和网络研究,我对这个查询感到疯狂。我想在 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吗?

【问题讨论】:

  • 奇怪的是,如果我使用像 mongoQM 这样的 ODM 包装器,那么距离搜索工作正常......

标签: php mongodb mongodb-query


【解决方案1】:

我在 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()' 附加到它以查看内部通常发生的情况数据库)。

【讨论】:

  • 感谢您的查找!不过,我非常相信 maxDistance 应该以“米”而不是弧度的形式提供,以防您像我一样使用 geoJSON 字段。
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2017-09-26
  • 2017-08-11
  • 2014-07-19
相关资源
最近更新 更多