INSERT {
"name": "Robert",
"surname": "Baratheon",
"alive": false,
"traits": ["A","H","C"]
} INTO Characters
FOR rel in data
LET parentId = FIRST(
FOR c IN Characters
FILTER c.name == rel.parent.name
FILTER c.surname == rel.parent.surname
LIMIT 1
RETURN c._id
)
LET childId = FIRST(
FOR c IN Characters
FILTER c.name == rel.child.name
FILTER c.surname == rel.child.surname
LIMIT 1
RETURN c._id
)
FILTER parentId != null AND childId != null
INSERT { _from: childId, _to: parentId } INTO ChildOf
RETURN NEW
RETURN DOCUMENT("Characters", ["2861650","2861653"])
FOR c IN Characters
RETURN { name: c.name, surname: c.surname }
FOR c IN Characters
RETURN DOCUMENT("Traits", c.traits)[*].en
FOR c IN Characters
RETURN MERGE(c, { traits: DOCUMENT("Traits", c.traits)[*].en } )
FOR x in UNION(
(FOR a in Characters
RETURN a.name),
(FOR b in Traits
RETURN b.name)
)
RETURN x
UPDATE "2861650" WITH { alive: false,age:null } IN Characters OPTIONS
{ keepNull: false }
//格式
upsert
insert
update
//示例
FOR i IN 1..1000
UPSERT { _key: CONCAT('test', i)}
INSERT {foobar: false}
UPDATE {_rev: "1287623", foobar: true }
IN users OPTIONS { ignoreRevs: false }
REPLACE "2861650" WITH {
name: "Ned",
surname: "Stark",
alive: false,
age: 41,
traits: ["A","H","C","N","P"]
} IN Characters
//删除characters中_key为2861650的元素
REMOVE "2861650" IN Characters
//增加options
FOR i IN 1..1000
REMOVE { _key: CONCAT('test', i) } IN users OPTIONS { ignoreErrors: true,
waitForSync: true,ignoreRevs: false }
//按class,size聚合,把name聚合到数组groups中
FOR u IN server1
COLLECT class = u.class,size=u.size INTO groups=u.name
RETURN {
"class" : class,
"size":size,
"group":groups
}
//按class,size聚合,把name,_id聚合到数组groups中,groups中每个元素是一个对象{name:u.name,id:u._id}
FOR u IN server1
COLLECT class = u.class,size=u.size INTO groups={name:u.name,id:u._id}
RETURN {
"class" : class,
"size":size,
"group":groups
}
// keep 表示object里面的key是变量名
FOR u IN server1
let name=u.name
let class1=u.class
COLLECT class = u.class,size=u.size INTO groups keep class1,name
RETURN {
"class" : class,
"size":size,
"group":groups
}
//计算个数
FOR u IN relations1
COLLECT WITH COUNT INTO length
RETURN length
//类似于
return length(relations1)
支持length,min,max,sum,average,stddev_population,stddev_sample,variance_population,variance_sample
FOR u IN server1
COLLECT size=u.size AGGREGATE minSize = MIN(u._id), maxSize = MAX(u._id)
RETURN {
size,
minSize,
maxSize
}
//类似于select name,age from Characters Where age<13
FOR c IN Characters
FILTER c.age < 13
RETURN { name: c.name, age: c.age }
FOR c IN Characters
FILTER c.age < 13 AND c.age != null
RETURN { name: c.name, age: c.age }
//等价于
FOR c IN Characters
FILTER c.age < 13
FILTER c.age != null
RETURN { name: c.name, age: c.age }
//类似于select name,surname from characters where name in (‘Jon’,’Joffrey’)
FOR c IN Characters
FILTER c.name == "Jon" OR c.name == "Joffrey"
RETURN { name: c.name, surname: c.surname }
FOR c IN Characters
LIMIT 5
RETURN c.name
//从第二个位置开始选5个,可以用于分页
FOR c IN Characters
LIMIT 2, 5
RETURN c.name
//DESC降序;ASC升序
FOR c IN Characters
FILTER c.surname
SORT c.surname, c.name DESC
LIMIT 10
RETURN {
surname: c.surname,
name: c.name
}
//OLD 与 NEW的使用,OLD是修改前的数据,NEW是修改后的数据
//OLD和NEW必须是大写
UPSERT { name: "test" }
INSERT { name: "test" }
UPDATE {name:"update" } IN users
LET opType = IS_NULL(OLD) ? "insert" : "update"
RETURN { _key: NEW._key, type: opType }
[WITH vertexCollection1[, vertexCollection2[, ...vertexCollectionN]]]
FOR vertex[, edge[, path]]
IN [min[..max]]
OUTBOUND|INBOUND|ANY startVertex
GRAPH graphName
[OPTIONS options]
//返回路径
FOR v, e, p IN 2..2 OUTBOUND "verts/A" edges
FILTER v._id == "verts/C"
RETURN CONCAT_SEPARATOR(" -> ", p.vertices[*]._key)
//双重循环
for v,e,p IN 1..100 OUTBOUND "server1/2646bd6f528c6a9b2176da28b0c6195c" graph 'product1'
for v1,e1,p1 IN 1..100 OUTBOUND "server1/c3ea0f7f7efcb5d9e7b9942331adc892" graph 'product1'
filter v._id==v1._id
return {from_edges:p.edges[*].name,from_vertices:p.vertices[*].name,to_edges:p1.edges[*].name,to_vertices:p1.vertices[*].name}
//inbound 逆序; outbound 顺序;any 任意方向
FOR v, e, p IN 1..5 OUTBOUND 'circles/A' GRAPH 'traversalGraph'
FILTER p.edges[0].theTruth == true
RETURN p
for v,e,p IN 1..10000 ANY "server1/2646bd6f528c6a9b2176da28b0c6195c" graph 'product1'
filter v.class=='服务器'
return {edges:p.edges[*].name,vertices:p.vertices[*].name}
//a..b 相当于python中的range(a,b+1)
//深度为2的孙子
FOR c IN Characters
FILTER c.name == "Tywin"
FOR v IN 2..2 INBOUND c ChildOf
RETURN v.name
//深度为1的父母
FOR v IN 1..1 OUTBOUND "Characters/2901776" ChildOf
RETURN v.name
//给遍历深度一个范围,至少走一步,至多走两步
FOR c IN Characters
FILTER c.name == "Joffrey"
FOR v IN 1..2 OUTBOUND c ChildOf
RETURN DISTINCT v.name
//设置index后,通过near来找到离(53.35,-6.26)最近的3个点
FOR loc IN NEAR(Locations, 53.35, -6.26, 3)
RETURN {
name: loc.name,
latitude: loc.coordinate[0],
longitude: loc.coordinate[1]
}
//返回距离目标点多少米
FOR loc IN NEAR(Locations, 53.35, -6.26, 3, "distance")
RETURN {
name: loc.name,
latitude: loc.coordinate[0],
longitude: loc.coordinate[1],
distance: loc.distance / 1000
}
FOR vertex[, edge]
IN OUTBOUND|INBOUND|ANY SHORTEST_PATH
startVertex TO targetVertex
edgeCollection1, ..., edgeCollectionN
[OPTIONS options]
//遍历
for a in relations1
let from_name= document(a._from).name
let to_name=document(a._to).name
return merge(a,{_from:from_name,_to:to_name})
ASQL中用//或者/**/注释
null < bool < number < string <
array/list < object/document
=~ !~
== !=
and == &&
or == ||
not == !
1 || 7 // 1
null || "foo" // "foo"
null && true // null
true && 23 // 23
[ 1, 2, 3 ] ALL IN [ 2, 3, 4 ] // false
[ 1, 2, 3 ] ALL IN [ 1, 2, 3 ] // true
[ 1, 2, 3 ] NONE IN [ 3 ] // false
[ 1, 2, 3 ] NONE IN [ 23, 42 ] // true
[ 1, 2, 3 ] ANY IN [ 4, 5, 6 ] // false
[ 1, 2, 3 ] ANY IN [ 1, 42 ] // true
[ 1, 2, 3 ] ANY == 2 // true
[ 1, 2, 3 ] ANY == 4 // false
[ 1, 2, 3 ] ANY > 0 // true
[ 1, 2, 3 ] ANY <= 1 // true
[ 1, 2, 3 ] NONE < 99 // false
[ 1, 2, 3 ] NONE > 10 // true
[ 1, 2, 3 ] ALL > 2 // false
[ 1, 2, 3 ] ALL > 0 // true
[ 1, 2, 3 ] ALL >= 3 // false
["foo", "bar"] ALL != "moo" // true
["foo", "bar"] NONE == "bar" // false
["foo", "bar"] ANY == "foo" // true
//用LET定义
LET name = "Peter"
LET age = 42
RETURN{name,age}
//@定义,查询框右边会出现 填写变量的框框
FOR u IN @collection
//FILTER u.active == true
RETURN u
POW()
CONCAT()
TO_NUMBER()
RANGE()
MERGE()//合并几个object
FLOOR()//向下取整
MIX()
MAX()
APPEND()//相当于UNION ALL
UNION()//相当于UNION