通用函数
| 名称 | 说明 |
CURRENT_DOCUMENT
|
返回流水线中当前正在处理的文档。 |
CONCAT
|
串联两个或多个相同类型的值。 |
LENGTH
|
计算 String、Bytes、Array、Vector 或 Map 的长度。
|
REVERSE
|
反转 String、Bytes 或 Array。
|
CURRENT_DOCUMENT
语法:
current_document() -> MAP
说明:
评估结果为一个映射,其中包含当前范围中定义的所有字段。当您要合并或汇总多个文档,或者想要动态检查文档中的字段名称时,此方法非常有用。
例如,如需获取按某个字段分组的文档列表,请执行以下操作:
Node.js
const cities = await db.pipeline()
.collection("/restaurants")
.aggregate({
groups: [ field("location.state").as("state") ],
accumulators: [ arrayAgg(currentDocument().as("restaurants")) ]
})
.execute();
CONCAT
语法:
concat[T <: STRING | BYTES | ARRAY](values:T ...) -> T
说明:
串联两个或多个相同类型的值。
示例:
| values | concat(values) |
|---|---|
| "abc"、"def" | "abcdef" |
| [1, 2]、[3, 4] | [1, 2, 3, 4] |
| b"abc"、b"def" | b"abcdef" |
| "abc"、[1,2,3]、"ghi" | 错误 |
| [1,2,3] | 错误 |
| "abc"、null | null |
Node.js
concat(constant("Author ID: "), field("authorId"));
Web
concat(constant("Author ID: "), field("authorId"));
Swift
let displayString = Constant("Author ID: ").concat([Field("authorId")])
Kotlin
val displayString = constant("Author ID: ").concat(field("authorId"))
Java
Expression displayString = constant("Author ID: ").concat(field("authorId"));
Python
Constant.of("Author ID: ").concat(Field.of("authorId"))
LENGTH
语法:
length[T <: STRING | BYTES | ARRAY | VECTOR | MAP](value: T) -> INT64
说明:
计算 String、Bytes、Array、Vector 或 Map 值的长度。
示例:
| 值 | length(value) |
|---|---|
| "hello" | 5 |
| [1, 2, 3, 4] | 4 |
| b"abcde" | 5 |
| null | null |
| 1 | 错误 |
REVERSE
语法:
reverse[T <: STRING | BYTES | ARRAY](value: T) -> T
说明:
反转 String、Bytes 或 Array 值。
示例:
| 值 | reverse(value) |
|---|---|
| "hello" | "olleh" |
| [1, 2, 3] | [3, 2, 1] |
| b"abc" | b"cba" |
| 23 | 错误 |
| null | null |