ฟังก์ชันทั่วไป

ฟังก์ชันทั่วไป

ชื่อ คำอธิบาย
CURRENT_DOCUMENT แสดงผลเอกสารที่กำลังประมวลผลใน Pipeline
CONCAT เชื่อมค่า 2 ค่าขึ้นไปที่มีประเภทเดียวกัน
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

คำอธิบาย:

เชื่อมค่า 2 ค่าขึ้นไปที่มีประเภทเดียวกัน

ตัวอย่าง:

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[T <: STRING | BYTES | ARRAY | VECTOR | MAP](value: T) -> INT64

คำอธิบาย:

คำนวณความยาวของค่า String, Bytes, Array, Vector หรือ Map

ตัวอย่าง:

value 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

ตัวอย่าง:

value reverse(value)
"hello" "olleh"
[1, 2, 3] [3, 2, 1]
b"abc" b"cba"
23 ข้อผิดพลาด
null null