High-level operations
操作符 | 含义 |
---|---|
FOR | 迭代数组的所有元素 |
RETURN | 生成查询结果 |
FILTER | 将结果限制为与任意逻辑条件匹配的元素 |
SORT | 强制排序一系列已经生成的中间结果 |
LIMIT | 将结果中的元素数量减少到最多指定的数字,可选择跳过元素(分页) |
LET | 为变量分配任意值 |
COLLECT | 按一个或多个组标准对数组进行分组。也可以统计和聚合 |
REMOVE | 从集合中删除文档 |
UPDATE | 部分更新集合中的文档 |
REPLACE | 完全替换集合中的文档 |
INSERT | 将新文档插入到集合中 |
UPSERT | 更新/替换现有文档,或在不存在的情况下创建它 |
WITH | 指定查询中使用的集合(仅在查询时开始) |
-
FOR
-
eg.
- 1 迭代静态声明的数组的值
FOR year IN [ 2011, 2012, 2013 ] RETURN { "year" : year, "isLeapYear" : year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) }
- 2 嵌套
FOR u IN Users FOR l IN location RETURN { "user" : u.name, "location" : l.location }
-
-
RETURN
- 基本语法
RETURN expression
- eg.
(支持动态属性名称)
FOR u IN Users RETURN { [ u._id ]: u.age }
-
返回列表(
DISTINCT
)- 语法
FOR variableName IN expression RETURN DISTINCT expression
⚠️要使数组或子查询的结果唯一,只需对数组或子查询应用DISTINCT
-
eg.
- 1(
子查询结果
应用DISTINCT)
FOR what IN 1..2 RETURN DISTINCT ( FOR i IN [ 1, 2, 3, 4, 1, 3 ] RETURN i )
- 2(子查询应用DISTINCT)
FOR what IN 1..2 LET sub = ( FOR i IN [ 1, 2, 3, 4, 1, 3 ] RETURN DISTINCT i ) RETURN sub
- 1(
-
COLLECT
-
可用于按一个或多个组条件对数组进行分组
-
COLLECT语句将消除当前范围内的所有局部变量
-
COLLECT后, 只有由COLLECT本身引入的变量是可用的
- 基本语法-1 ⬇️
将结果按表达式中指定的组条件分组
```javascript COLLECT variableName = expression options ``` - eg. ```javascript FOR u IN students COLLECT class = u.class RETURN { "class" : class } ```
- 基本语法-2 ⬇️
第二中形式与第一种形式的功能相同,但它还引入了一个变量(由groupsvariable指定),该变量包含属于该组的所有元素
```javascript COLLECT variableName = expression INTO groupsVariable options ``` - eg. - 1 ```javascript FOR u IN students COLLECT class = u.class INTO groups RETURN { "class" : class, "studentsInClass" : groups } ``` - 2 ```javascript FOR u IN students COLLECT class= u.class, course = u.course INTO groups RETURN { "class" : class, "course" : course, "studentInClass" : groups } ```
- 基本语法-3 ⬇️
第三种形式的collect允许使用任意projectionexpression重写groupsvariable的内容
```javascript COLLECT variableName = expression INTO groupsVariable = projectionExpression options ``` - eg. 允许改写groups内容和名字 - 1 ```javascript FOR u IN students COLLECT class= u.class, course = u.course INTO groups = u.name RETURN { "class" : class, "course" : course, "studentsNames" : groups } ``` - 2 ```javascript FOR u IN students COLLECT class= u.class, course = u.course INTO groups = { "Name": u.name, "Grades": u.grades } RETURN { "class" : class, "course" : course, "studentsGrades" : groups } ```
- 基本语法-4 ⬇️
COLLECT还提供一个可选的KEEP子句, 可用于
控制将哪些变量复制到创建的变量中
)注:
如果未指定keep子句,则作用域中的所有变量都将作为子属性复制到groupsvariable中```javascript COLLECT variableName = expression INTO groupsVariable KEEP keepVariable options ``` - eg. ```javascript FOR u IN students LET name = u.name LET someCalculation = u.grades + 10 COLLECT class = u.class INTO groups KEEP name RETURN { "class" : class, "userNames" : groups[*].name , "upGrades" : groups[*].someCalculation } ```
- 基本语法-5 ⬇️
collect还提供了一个特殊的 WITH COUNT 子句,可用于有效地确定组成员的数量。
```javascript COLLECT variableName = expression WITH COUNT INTO countVariable options ``` - eg. - 1 ```javascript FOR u IN students COLLECT WITH COUNT INTO length RETURN length ``` - 2 计算每个组中的项数 ```javascript FOR u IN students COLLECT age = u.age WITH COUNT INTO length RETURN { "age" : age, "count" : length } ```
- 基本语法-6 ⬇️
collect语句可用于执行每个组的数据聚合
```javascript COLLECT variableName = expression AGGREGATE variableName = aggregateExpression options ``` - eg. ```javascript FOR u IN students COLLECT class = u.class AGGREGATE minGrades = MIN(u.grades), maxGrades = MAX(u.grades) RETURN { class, minGrades, maxGrades } ``` [tips](https://git.belstar.com.cn/bel-star/mis/xian/intern/blob/Linda/ArangoDB%E5%88%86%E4%BA%AB%EF%BC%88%E4%BA%8C%EF%BC%89.md)
-
Array Operators
-
[*]
数组扩展 ➡ 为了️访问数组中所有元素的命名属性
- 1
-
1.1
FOR u IN Users RETURN { name: u.name, friends: u.friends[*].name }
-
1.2
FOR u IN Users RETURN { name: u.name, friends: (FOR f IN u.friends RETURN f.name) }
-
- 1
-
[**]
数组收缩 ➡️为了折叠(或展平)嵌套数组中的结果
-
before
-
1
FOR u IN Users RETURN u.friends[*].name
-
2
RETURN ( FOR u IN Users RETURN u.friends[*].name )
-
-
use
RETURN ( FOR u IN Users RETURN u.friends[*].name )[**]
-