共用方式為


地理空間查詢支援

地理空間資料現在可以使用 Azure DocumentDB 來儲存與查詢。 此強化提供強大工具以管理與分析空間資料,支援即時位置追蹤、路線優化及空間分析等多種應用。

以下是目前支援的地理空間指令與操作員的簡要概述:

地理空間查詢運算元

$geoIntersects

選擇指定幾何與文件幾何相交的文件。 對於尋找與某一幾何形狀共享任何空間區域的文件非常有用。

   db.collection.find({
       location: {
           $geoIntersects: {
               $geometry: {
                   type: "<GeoJSON object type>",
                   coordinates: [[[...], [...], [...], [...]]]
               }
           }
       }
   })

$geoWithin

選擇包含完全存在於指定形狀內的地理空間資料的文件。 此運算子用於在指定區域內尋找文件。

   db.collection.find({
       location: {
           $geoWithin: {
               $geometry: {
                   type: "Polygon",
                   coordinates: [[[...], [...], [...], [...]]]
               }
           }
       }
   })

$box

以兩個座標對(左下角與右上角)定義矩形區域。 與操作符搭配 $geoWithin 使用,可在此矩形內尋找文件。 例如,在地圖上尋找一個矩形區域內的所有位置。

  db.collection.find({
      location: {
          $geoWithin: {
              $box: [[lowerLeftLong, lowerLeftLat], [upperRightLong, upperRightLat]]
          }
      }
  })

$center

以中心點和弧度半徑定義一個圓形區域。 與 $geoWithin 操作員一起使用,用來尋找此圈內的文件。

  db.collection.find({
      location: {
          $geoWithin: {
              $center: [[longitude, latitude], radius]
          }
      }
  })

$centerSphere

類似 $center於 ,但以中心點和弧度半徑定義球面面積。 對球面幾何計算非常有用。

  db.collection.find({
      location: {
          $geoWithin: {
              $centerSphere: [[longitude, latitude], radius]
          }
      }
  })

$geometry

指定一個 GeoJSON 物件來定義幾何體。 結合地理空間運算子執行基於複雜形狀的查詢。

  db.collection.find({
      location: {
          $geoIntersects: {
              $geometry: {
                  type: "<GeoJSON object type>",
                  coordinates: [longitude, latitude]
              }
          }
      }
  })

$maxDistance

指定地理空間查詢中點與點的最大距離。 與 $near$nearSphere 和 運算子一起使用。 例如,找到一個點 2 公里範圍內的所有位置。

  db.collection.find({
      location: {
          $near: {
              $geometry: {
                  type: "Point",
                  coordinates: [longitude, latitude]
              },
              $maxDistance: distance
          }
      }
  })

$minDistance

指定地理空間查詢中點距的最小距離。 與 $near$nearSphere 和 運算子一起使用。

  db.collection.find({
      location: {
          $near: {
              $geometry: {
                  type: "Point",
                  coordinates: [longitude, latitude]
              },
              $minDistance: distance
          }
      }
  })

$polygon

利用座標對陣列定義多邊形。 與操作符搭配 $geoWithin 使用,在該多邊形內尋找文件。

  db.collection.find({
      location: {
          $geoWithin: {
              $geometry: {
                  type: "Polygon",
                  coordinates: [[[...], [...], [...], [...]]]
              }
          }
      }
  })

$near

尋找靠近指定地點的文件。 回傳文件依距離點的距離排序。 例如,尋找離使用者所在位置最近的餐廳。

    db.collection.find({
        location: {
            $near: {
                $geometry: {
                    type: "Point",
                    coordinates: [longitude, latitude]
                },
                $maxDistance: distance
            }
        }
    })

$nearSphere

類似 $near於 ,但是在球面上進行計算。 對於地球表面更精確的距離計算非常有用。

    db.collection.find({
        location: {
            $nearSphere: {
                $geometry: {
                    type: "Point",
                    coordinates: [longitude, latitude]
                },
                $maxDistance: distance
            }
        }
    })

地理空間聚合階段

$geoNear

執行地理空間查詢,返回依距離指定點排序的文件。 可包含額外的查詢條件及返回距離資訊。

  db.collection.aggregate([
      {
          $geoNear: {
              near: {
                  type: "Point",
                  coordinates: [longitude, latitude]
              },
              distanceField: "distance",
              spherical: true
          }
      }
  ])

考量與未支援功能

  • 目前,不支援使用面積超過單一半球的單環 GeoJSON 多邊形查詢。 在這種情況下,Azure DocumentDB 會回傳以下錯誤訊息:

    Error: Custom CRS for big polygon is not supported yet.
    
  • 不允許使用一般索引和地理空間索引的複合索引。 例如:

    db.collection.createIndex({a: "2d", b: 1});
    Error: Compound 2d indexes are not supported yet
    
  • 目前不支援帶有洞穴的多邊形用於$geoWithin查詢。 雖然插入帶有孔洞的多邊形不受限制,但最終會因以下錯誤訊息而失敗:

    Error: $geoWithin currently doesn't support polygons with holes
    
  • 關鍵欄位在$geoNear彙整階段始終是必需的。 若缺少金鑰欄位,則會出現以下錯誤:

    Error: $geoNear requires a 'key' option as a String
    
  • $geoNear這些 、 $near$nearSphere 階段沒有嚴格的索引要求,因此即使缺少索引,這些查詢不會失敗。