MongoDB在php中查找带有$and $or和$nearSphere查询


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

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

我在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(("附加到它以查看数据库中通常会发生什么(。