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