collection
座標包括經度和緯度,可以將兩者存儲爲一個屬性,也可以分開爲兩個屬性存儲。ArangoDB允許座標查詢。所以今天的主要內容時創建座標和查詢座標。
Create Collection
-
創建數據
數據來自各城市的座標數據。
- 創建集合Collection(比如命名爲Locations)
- 插入數據
let places = [ {"name":"Dragostone","coordinate":[55.167801,-6.815096]}, {"name":"King's Landing","coordinate":[42.639752,18.110189]}, {"name":"The Red Keep","coordinate":[35.896447,14.446442]}, {"name":"Winterfell","coordinate":[54.368321,-5.581312]}, {"name":"Vaes Dothrak","coordinate":[54.16776,-6.096125]}, {"name":"Beyond the wall","coordinate":[64.265473,-21.094093]}, {"name":"Yukai","coordinate":[31.046642,-7.129532]}, {"name":"Astapor","coordinate":[31.50974,-9.774249]} ] for item in places insert item into Locations return NEW
-
創建座標索引
創建座標索引的步驟如下:- Go to COLLECTIONS
- Click on the Locations collection
- Switch to the Indexes tab at top
- Click the geen button with a plus on the right -hand side
- Change the type to Geo Index
- Enter coordinate into the Fields field
- Click Create to confirm
Query Collection
-
NEAR()
- 語法 :
NEAR(collection, latitude, longitude, limit, distanceName)
- 🌰
- 帶
collection
,latitude
,longitude
,limit
參數FOR item IN NEAR(Locations, 55.167801, -6.815096,4) RETURN item
- 帶
collection
,latitude
,longitude
,limit
,distanceName
參數FOR item IN NEAR(Locations, 55.167801, -6.815096,4,"distName") RETURN item
- 帶
- 語法 :
-
WITHIN()
-
語法:
WITHIN(coll, latitude, longitude, radius, distanceName)
-
🌰
FOR item IN WITHIN(Locations, 55.167801, -6.815096, 120000,"dist") RETURN item
注意
:3.4.0+ 版本中很少使用NEAR()
和WITHIN()
,而是用DISTANCE()
來代替,接下來看下DISTANCE()
: -
-
DISTANCE()
-
語法:
DISTANCE(latitude1, longitude1, latitude2, longitude2)
-
🌰
FOR item IN Locations SORT DISTANCE(doc.latitude, doc.longitude, 55.167801, -6.815096) limit 3 RETURN item
-
綜合案例——A geo demonstration using Foxx
- Preparations
- Installation Foxx
Arangodb Web UI—— Services——add Service——Upload——導入下載的.zip格式文件
- 創建集合
restaurants
和neighborhoods
- 創建索引
restaurants
:(type: “geo”, fields: [ “location” ], geoJson:true )neighborhoods
(type: “geo”, fields: [ “geometry” ], geoJson:true )
- 導入數據
分別向2個集合中導入數據(下載文件的
data
文件中)
- Installation Foxx
- Usage