MongoDB空間查詢

來源:互聯網
上載者:User

MongoDB 在2.4版本以後,對空間查詢支援更友好了,下面簡介一下$geoWithin,文章翻譯自:http://docs.mongodb.org/manual/reference/operator/geoWithin/#op._S_geoWithin

$geoWithin

      在MongoDB2.4中使用$geoWithin操作符(或稱函數,下亦同)替代廢棄的$within操作符。$geoWithin操作符是一個支援查詢在一個幾何要素內(完全在這幾何要素內)的另一個特定的點、線或者其他幾何類型要素。$geoWithin操作符支援GeoJSON作為查詢條件。$geoWithin操作符不返回排序的結果集。它的查詢效率要比支援排序的$near或者$nearSphere操作符快。

     $geoWithin支援空間索引。和2.2.3版本$geoWithin不同的是,它需要空間索引,這樣可以提升空間查詢效率。

     查詢一個多邊形內部所有要素的文法如下:

   db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $geometry :
                               { type : "Polygon" ,
                                 coordinates : [ [ [ <lng1>, <lat1> ] , [ <lng2>, <lat2> ] ... ] ]
                      } } } } )

需要特別聲明的是:座標的順序必須這樣,“經度,維度”。

下面的例子是查詢一個多邊形範圍內所有索引的地名要素。

db.places.find( { loc :                  { $geoWithin :                    { $geometry :                      { type : "Polygon" ,                        coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]                } } } } )

對於幾何查詢, 可以參考:

  • $box
  • $polygon
  • $center (定義一個圓)
  • $centerSphere (定義一個橢球面上的圓)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.