รวม
ฟังก์ชันการรวมทั้งหมดสามารถใช้เป็นนิพจน์ระดับบนสุดในaggregate(...) stage ได้
| ชื่อ | คำอธิบาย |
COUNT
|
แสดงผลจำนวนเอกสาร |
COUNT_IF
|
แสดงผลจำนวนเอกสารที่นิพจน์ประเมินเป็น TRUE
|
COUNT_DISTINCT
|
แสดงผลจำนวนค่าที่ไม่ซ้ำกันซึ่งไม่ใช่ NULL
|
SUM
|
แสดงผลรวมของค่า NUMERIC ทั้งหมด
|
AVERAGE
|
แสดงผลค่าเฉลี่ยของค่า NUMERIC ทั้งหมด
|
MINIMUM
|
แสดงผลค่าที่ไม่ใช่ NULL ที่ต่ำที่สุด
|
MAXIMUM
|
แสดงผลค่าสูงสุดที่ไม่ใช่ NULL
|
FIRST
|
แสดงผลค่า expression สำหรับเอกสารแรก
|
LAST
|
แสดงผลค่า expression สำหรับเอกสารสุดท้าย
|
ARRAY_AGG
|
แสดงผลอาร์เรย์ของค่าอินพุตทั้งหมด |
ARRAY_AGG_DISTINCT
|
แสดงผลอาร์เรย์ของค่าอินพุตที่ไม่ซ้ำกันทั้งหมด |
COUNT
ไวยากรณ์:
count() -> INT64
count(expression: ANY) -> INT64
คำอธิบาย:
แสดงผลจำนวนเอกสารจากขั้นตอนก่อนหน้าซึ่ง expression
ประเมินค่าเป็นค่าที่ไม่ใช่ NULL หากไม่ได้ระบุ expression ระบบจะแสดงจำนวนเอกสารทั้งหมดจากขั้นตอนก่อนหน้า
Node.js
// Total number of books in the collection const countOfAll = await db.pipeline() .collection("books") .aggregate(countAll().as("count")) .execute(); // Number of books with nonnull `ratings` field const countField = await db.pipeline() .collection("books") .aggregate(field("ratings").count().as("count")) .execute();
Web
// Total number of books in the collection const countOfAll = await execute(db.pipeline() .collection("books") .aggregate(countAll().as("count")) ); // Number of books with nonnull `ratings` field const countField = await execute(db.pipeline() .collection("books") .aggregate(field("ratings").count().as("count")) );
Swift
// Total number of books in the collection let countAll = try await db.pipeline() .collection("books") .aggregate([CountAll().as("count")]) .execute() // Number of books with nonnull `ratings` field let countField = try await db.pipeline() .collection("books") .aggregate([Field("ratings").count().as("count")]) .execute()
Kotlin
// Total number of books in the collection val countAll = db.pipeline() .collection("books") .aggregate(AggregateFunction.countAll().alias("count")) .execute() // Number of books with nonnull `ratings` field val countField = db.pipeline() .collection("books") .aggregate(AggregateFunction.count("ratings").alias("count")) .execute()
Java
// Total number of books in the collection Task<Pipeline.Snapshot> countAll = db.pipeline() .collection("books") .aggregate(AggregateFunction.countAll().alias("count")) .execute(); // Number of books with nonnull `ratings` field Task<Pipeline.Snapshot> countField = db.pipeline() .collection("books") .aggregate(AggregateFunction.count("ratings").alias("count")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Count # Total number of books in the collection count_all = ( client.pipeline().collection("books").aggregate(Count().as_("count")).execute() ) # Number of books with nonnull `ratings` field count_field = ( client.pipeline() .collection("books") .aggregate(Count("ratings").as_("count")) .execute() )
Java
// Total number of books in the collection Pipeline.Snapshot countAll = firestore.pipeline().collection("books").aggregate(countAll().as("count")).execute().get(); // Number of books with nonnull `ratings` field Pipeline.Snapshot countField = firestore .pipeline() .collection("books") .aggregate(count("ratings").as("count")) .execute() .get();
COUNT_IF
ไวยากรณ์:
count_if(expression: BOOLEAN) -> INT64
คำอธิบาย:
แสดงจำนวนเอกสารจากขั้นตอนก่อนหน้าซึ่ง expression
ประเมินเป็น TRUE
Node.js
const result = await db.pipeline() .collection("books") .aggregate( field("rating").greaterThan(4).countIf().as("filteredCount") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .aggregate( field("rating").greaterThan(4).countIf().as("filteredCount") ) );
Swift
let result = try await db.pipeline() .collection("books") .aggregate([ AggregateFunction("count_if", [Field("rating").greaterThan(4)]).as("filteredCount") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .aggregate( AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCount") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate( AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCount") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("rating").greater_than(4).count_if().as_("filteredCount")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countIf(field("rating").greaterThan(4)).as("filteredCount")) .execute() .get();
COUNT_DISTINCT
ไวยากรณ์:
count_distinct(expression: ANY) -> INT64
คำอธิบาย:
แสดงผลจำนวนค่าที่ไม่ซ้ำกันซึ่งไม่ใช่ NULL และไม่ใช่ ABSENT ของ expression
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("author").countDistinct().as("unique_authors")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("author").countDistinct().as("unique_authors")) );
Swift
let result = try await db.pipeline() .collection("books") .aggregate([AggregateFunction("count_distinct", [Field("author")]).as("unique_authors")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.countDistinct("author").alias("unique_authors")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate(AggregateFunction.countDistinct("author").alias("unique_authors")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("author").count_distinct().as_("unique_authors")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countDistinct("author").as("unique_authors")) .execute() .get();
SUM
ไวยากรณ์:
sum(expression: ANY) -> NUMBER
คำอธิบาย:
แสดงผลรวมของค่าตัวเลขทั้งหมด โดยละเว้นค่าที่ไม่ใช่ตัวเลข แสดงผล
NaN หากมีค่าเป็น NaN
เอาต์พุตจะมีประเภทเดียวกับประเภทอินพุตที่กว้างที่สุด ยกเว้นในกรณีต่อไปนี้
- ระบบจะแปลง
INTEGERเป็นDOUBLEหากแสดงเป็นINTEGERไม่ได้
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").sum().as("totalPopulation")) .execute();
Web
const result = await execute(db.pipeline() .collection("cities") .aggregate(field("population").sum().as("totalPopulation")) );
Swift
let result = try await db.pipeline() .collection("cities") .aggregate([Field("population").sum().as("totalPopulation")]) .execute()
Kotlin
val result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.sum("population").alias("totalPopulation")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.sum("population").alias("totalPopulation")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("cities") .aggregate(Field.of("population").sum().as_("totalPopulation")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(sum("population").as("totalPopulation")) .execute() .get();
AVERAGE
ไวยากรณ์:
average(expression: ANY) -> FLOAT64
คำอธิบาย:
แสดงผลค่าเฉลี่ยสำหรับค่าตัวเลขทั้งหมด โดยละเว้นค่าที่ไม่ใช่ตัวเลข
ประเมินเป็น NaN หากค่าใดๆ เป็น NaN หรือ NULL หากไม่มีการรวบรวมค่าตัวเลข
เอาต์พุตจะมีประเภทเดียวกับประเภทอินพุต ยกเว้นในกรณีต่อไปนี้
- ระบบจะแปลง
INTEGERเป็นDOUBLEหากแสดงเป็นINTEGERไม่ได้
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").average().as("averagePopulation")) .execute();
Web
const result = await execute(db.pipeline() .collection("cities") .aggregate(field("population").average().as("averagePopulation")) );
Swift
let result = try await db.pipeline() .collection("cities") .aggregate([Field("population").average().as("averagePopulation")]) .execute()
Kotlin
val result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.average("population").alias("averagePopulation")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.average("population").alias("averagePopulation")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("cities") .aggregate(Field.of("population").average().as_("averagePopulation")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(average("population").as("averagePopulation")) .execute() .get();
ขั้นต่ำ
ไวยากรณ์:
minimum(expression: ANY) -> ANY
คำอธิบาย:
แสดงผลค่าต่ำสุดที่ไม่ใช่ NULL และไม่ใช่ค่าที่ไม่มีของ expression เมื่อประเมินในแต่ละเอกสาร
หากไม่มีค่าที่ไม่ใช่ NULL และไม่ใช่ค่าที่ไม่มีอยู่ ระบบจะแสดงผล NULL ซึ่งรวมถึงในกรณีที่ไม่มีการพิจารณาเอกสาร
หากมีค่าเทียบเท่าขั้นต่ำหลายค่า ระบบจะแสดงค่าใดค่าหนึ่ง การจัดลำดับประเภทค่าจะเป็นไปตามการจัดลำดับที่ระบุไว้
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) );
Swift
let result = try await db.pipeline() .collection("books") .aggregate([Field("price").minimum().as("minimumPrice")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.minimum("price").alias("minimumPrice")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate(AggregateFunction.minimum("price").alias("minimumPrice")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("price").minimum().as_("minimumPrice")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(minimum("price").as("minimumPrice")) .execute() .get();
สูงสุด
ไวยากรณ์:
maximum(expression: ANY) -> ANY
คำอธิบาย:
แสดงผลค่าสูงสุดที่ไม่ใช่ NULL และไม่ใช่ค่าที่ไม่มีของ expression เมื่อประเมินในแต่ละเอกสาร
หากไม่มีค่าที่ไม่ใช่ NULL และไม่ใช่ค่าที่ไม่มีอยู่ ระบบจะแสดงผล NULL ซึ่งรวมถึงในกรณีที่ไม่มีการพิจารณาเอกสาร
หากมีค่าเทียบเท่าสูงสุดหลายค่า ระบบจะแสดงค่าใดค่าหนึ่ง การจัดลำดับประเภทค่าจะเป็นไปตามการจัดลำดับที่ระบุไว้
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) );
Swift
let result = try await db.pipeline() .collection("books") .aggregate([Field("price").maximum().as("maximumPrice")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.maximum("price").alias("maximumPrice")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate(AggregateFunction.maximum("price").alias("maximumPrice")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("price").maximum().as_("maximumPrice")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(maximum("price").as("maximumPrice")) .execute() .get();
FIRST
ไวยากรณ์:
first(expression: ANY) -> ANY
คำอธิบาย:
แสดงค่าของ expression สำหรับเอกสารแรกที่แสดง
สุดท้าย
ไวยากรณ์:
last(expression: ANY) -> ANY
คำอธิบาย:
แสดงค่าของ expression สำหรับเอกสารที่แสดงผลล่าสุด
ARRAY_AGG
ไวยากรณ์:
array_agg(expression: ANY) -> ARRAY<ANY>
คำอธิบาย:
แสดงผลอาร์เรย์ที่มีค่าทั้งหมดของ expression เมื่อประเมินในแต่ละเอกสาร
หากนิพจน์เปลี่ยนเป็นค่าที่ไม่มีอยู่ ระบบจะแปลงเป็น NULL
ลำดับขององค์ประกอบในอาร์เรย์เอาต์พุตไม่คงที่และไม่ควรนำไปใช้
ARRAY_AGG_DISTINCT
ไวยากรณ์:
array_agg_distinct(expression: ANY) -> ARRAY<ANY>
คำอธิบาย:
แสดงผลอาร์เรย์ที่มีค่าที่ไม่ซ้ำทั้งหมดของ expression เมื่อประเมินในแต่ละเอกสาร
หากนิพจน์เปลี่ยนเป็นค่าที่ไม่มีอยู่ ระบบจะแปลงเป็น NULL
ลำดับขององค์ประกอบในอาร์เรย์เอาต์พุตไม่คงที่และไม่ควรนำไปใช้
ฟังก์ชันทางคณิตศาสตร์
ฟังก์ชันทางคณิตศาสตร์ทั้งหมดใน Cloud Firestore มีลักษณะการทำงานดังนี้
- ประเมินเป็น
NULLหากพารามิเตอร์อินพุตใดเป็นNULL - ประเมินเป็น
NaNหากอาร์กิวเมนต์ใดอาร์กิวเมนต์หนึ่งเป็นNaN - สร้างข้อผิดพลาดหากเกิดการล้นหรือการขาด
นอกจากนี้ เมื่อฟังก์ชันเลขคณิตรับอาร์กิวเมนต์ตัวเลขหลายรายการที่มี
ประเภทต่างกัน (เช่น add(5.0, 6)) Cloud Firestore จะแปลงอาร์กิวเมนต์เป็นประเภทอินพุตที่กว้างที่สุดโดยนัย
หากระบุเฉพาะอินพุต INT32 ประเภทการแสดงผลจะเป็น INT64
| ชื่อ | คำอธิบาย |
ABS
|
แสดงผลค่าสัมบูรณ์ของ number
|
ADD
|
แสดงผลค่าของ x + y
|
SUBTRACT
|
แสดงผลค่าของ x - y
|
MULTIPLY
|
แสดงผลค่าของ x * y
|
DIVIDE
|
แสดงผลค่าของ x / y
|
MOD
|
แสดงผลเศษของการหาร x / y
|
CEIL
|
แสดงผลค่าเพดานของ number
|
FLOOR
|
แสดงผลค่าที่ปัดเศษลงของ number
|
ROUND
|
ปัดเศษ number เป็นทศนิยม places ตำแหน่ง
|
TRUNC
|
ตัดทศนิยมของ number ให้เหลือ places ตำแหน่ง
|
POW
|
แสดงผลค่าของ base^exponent
|
SQRT
|
แสดงผลรากที่สองของ number
|
EXP
|
แสดงผลจำนวนออยเลอร์ยกกำลัง exponent
|
LN
|
แสดงผลลอการิทึมธรรมชาติของ number
|
LOG
|
แสดงผลลอการิทึมของ number
|
LOG10
|
แสดงผลลอการิทึมของ number เป็นฐาน 10
|
RAND
|
แสดงผลเลขจุดลอยตัวแบบสุ่มเทียม |
ABS
ไวยากรณ์:
abs[N <: INT32 | INT64 | FLOAT64](number: N) -> N
คำอธิบาย:
แสดงผลค่าสัมบูรณ์ของ number
- แสดงข้อผิดพลาดเมื่อฟังก์ชันจะล้นค่า
INT32หรือINT64
ตัวอย่าง
| ตัวเลข | abs(number) |
|---|---|
| 10 | 10 |
| -10 | 10 |
| 10L | 10L |
| -0.0 | 0.0 |
| 10.5 | 10.5 |
| -10.5 | 10.5 |
| -231 | [error] |
| -263 | [error] |
เพิ่ม
ไวยากรณ์:
add[N <: INT32 | INT64 | FLOAT64](x: N, y: N) -> N
คำอธิบาย:
แสดงผลค่าของ x + y
ตัวอย่าง
| x | y | add(x, y) |
|---|---|---|
| 20 | 3 | 23 |
| 10.0 | 1 | 11.0 |
| 22.5 | 2.0 | 24.5 |
| INT64.MAX | 1 | [error] |
| INT64.MIN | -1 | [error] |
Node.js
const result = await db.pipeline() .collection("books") .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("soldBooks").add(Field("unsoldBooks")).as("totalBooks")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(Expression.add(field("soldBooks"), field("unsoldBooks")).alias("totalBooks")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.add(field("soldBooks"), field("unsoldBooks")).alias("totalBooks")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("soldBooks").add(Field.of("unsoldBooks")).as_("totalBooks")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(add(field("soldBooks"), field("unsoldBooks")).as("totalBooks")) .execute() .get();
ลบ
ไวยากรณ์:
subtract[N <: INT32 | INT64 | FLOAT64](x: N, y: N) -> N
คำอธิบาย:
แสดงผลค่าของ x - y
ตัวอย่าง
| x | y | subtract(x, y) |
|---|---|---|
| 20 | 3 | 17 |
| 10.0 | 1 | 9.0 |
| 22.5 | 2.0 | 20.5 |
| INT64.MAX | -1 | [error] |
| INT64.MIN | 1 | [error] |
Node.js
const storeCredit = 7; const result = await db.pipeline() .collection("books") .select(field("price").subtract(constant(storeCredit)).as("totalCost")) .execute();
Web
const storeCredit = 7; const result = await execute(db.pipeline() .collection("books") .select(field("price").subtract(constant(storeCredit)).as("totalCost")) );
Swift
let storeCredit = 7 let result = try await db.pipeline() .collection("books") .select([Field("price").subtract(Constant(storeCredit)).as("totalCost")]) .execute()
Kotlin
val storeCredit = 7 val result = db.pipeline() .collection("books") .select(Expression.subtract(field("price"), storeCredit).alias("totalCost")) .execute()
Java
int storeCredit = 7; Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.subtract(field("price"), storeCredit).alias("totalCost")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field store_credit = 7 result = ( client.pipeline() .collection("books") .select(Field.of("price").subtract(store_credit).as_("totalCost")) .execute() )
Java
int storeCredit = 7; Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(subtract(field("price"), storeCredit).as("totalCost")) .execute() .get();
MULTIPLY
ไวยากรณ์:
multiply[N <: INT32 | INT64 | FLOAT64](x: N, y: N) -> N
คำอธิบาย:
แสดงผลค่าของ x * y
ตัวอย่าง
| x | y | multiply(x, y) |
|---|---|---|
| 20 | 3 | 60 |
| 10.0 | 1 | 10.0 |
| 22.5 | 2.0 | 45.0 |
| INT64.MAX | 2 | [error] |
| INT64.MIN | 2 | [error] |
| FLOAT64.MAX | FLOAT64.MAX | +inf |
Node.js
const result = await db.pipeline() .collection("books") .select(field("price").multiply(field("soldBooks")).as("revenue")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("price").multiply(field("soldBooks")).as("revenue")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("price").multiply(Field("soldBooks")).as("revenue")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(Expression.multiply(field("price"), field("soldBooks")).alias("revenue")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.multiply(field("price"), field("soldBooks")).alias("revenue")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("price").multiply(Field.of("soldBooks")).as_("revenue")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(multiply(field("price"), field("soldBooks")).as("revenue")) .execute() .get();
DIVIDE
ไวยากรณ์:
divide[N <: INT32 | INT64 | FLOAT64](x: N, y: N) -> N
คำอธิบาย:
แสดงผลค่าของ x / y การหารจำนวนเต็มจะถูกตัดทอน
ตัวอย่าง
| x | y | divide(x, y) |
|---|---|---|
| 20 | 3 | 6 |
| 10.0 | 3 | 3.333... |
| 22.5 | 2 | 11.25 |
| 10 | 0 | [error] |
| 1.0 | 0.0 | +inf |
| -1.0 | 0.0 | -inf |
Node.js
const result = await db.pipeline() .collection("books") .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("ratings").divide(Field("soldBooks")).as("reviewRate")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(Expression.divide(field("ratings"), field("soldBooks")).alias("reviewRate")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.divide(field("ratings"), field("soldBooks")).alias("reviewRate")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("ratings").divide(Field.of("soldBooks")).as_("reviewRate")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(divide(field("ratings"), field("soldBooks")).as("reviewRate")) .execute() .get();
ม็อด
ไวยากรณ์:
mod[N <: INT32 | INT64 | FLOAT64](x: N, y: N) -> N
คำอธิบาย:
แสดงผลเศษของ x / y
- แสดง
errorเมื่อyเป็น 0 สำหรับประเภทจำนวนเต็ม (INT64) - แสดงผล
NaNเมื่อyเป็น 0 สำหรับประเภททศนิยม (FLOAT64)
ตัวอย่าง
| x | y | mod(x, y) |
|---|---|---|
| 20 | 3 | 2 |
| -10 | 3 | -1 |
| 10 | -3 | 1 |
| -10 | -3 | -1 |
| 10 | 1 | 0 |
| 22.5 | 2 | 0.5 |
| 22.5 | 0.0 | NaN |
| 25 | 0 | [error] |
Node.js
const displayCapacity = 1000; const result = await db.pipeline() .collection("books") .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) .execute();
Web
const displayCapacity = 1000; const result = await execute(db.pipeline() .collection("books") .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) );
Swift
let displayCapacity = 1000 let result = try await db.pipeline() .collection("books") .select([Field("unsoldBooks").mod(Constant(displayCapacity)).as("warehousedBooks")]) .execute()
Kotlin
val displayCapacity = 1000 val result = db.pipeline() .collection("books") .select(Expression.mod(field("unsoldBooks"), displayCapacity).alias("warehousedBooks")) .execute()
Java
int displayCapacity = 1000; Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.mod(field("unsoldBooks"), displayCapacity).alias("warehousedBooks")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field display_capacity = 1000 result = ( client.pipeline() .collection("books") .select(Field.of("unsoldBooks").mod(display_capacity).as_("warehousedBooks")) .execute() )
Java
int displayCapacity = 1000; Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(mod(field("unsoldBooks"), displayCapacity).as("warehousedBooks")) .execute() .get();
CEIL
ไวยากรณ์:
ceil[N <: INT32 | INT64 | FLOAT64](number: N) -> N
คำอธิบาย:
แสดงค่าจำนวนเต็มที่น้อยที่สุดซึ่งไม่น้อยกว่า number
ตัวอย่าง
| ตัวเลข | ceil(number) |
|---|---|
| 20 | 20 |
| 10 | 10 |
| 0 | 0 |
| 24L | 24L |
| -0.4 | -0.0 |
| 0.4 | 1.0 |
| 22.5 | 23.0 |
+inf |
+inf |
-inf |
-inf |
Node.js
const booksPerShelf = 100; const result = await db.pipeline() .collection("books") .select( field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") ) .execute();
Web
const booksPerShelf = 100; const result = await execute(db.pipeline() .collection("books") .select( field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") ) );
Swift
let booksPerShelf = 100 let result = try await db.pipeline() .collection("books") .select([ Field("unsoldBooks").divide(Constant(booksPerShelf)).ceil().as("requiredShelves") ]) .execute()
Kotlin
val booksPerShelf = 100 val result = db.pipeline() .collection("books") .select( Expression.divide(field("unsoldBooks"), booksPerShelf).ceil().alias("requiredShelves") ) .execute()
Java
int booksPerShelf = 100; Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( Expression.divide(field("unsoldBooks"), booksPerShelf).ceil().alias("requiredShelves") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field books_per_shelf = 100 result = ( client.pipeline() .collection("books") .select( Field.of("unsoldBooks") .divide(books_per_shelf) .ceil() .as_("requiredShelves") ) .execute() )
Java
int booksPerShelf = 100; Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(ceil(divide(field("unsoldBooks"), booksPerShelf)).as("requiredShelves")) .execute() .get();
FLOOR
ไวยากรณ์:
floor[N <: INT32 | INT64 | FLOAT64](number: N) -> N
คำอธิบาย:
แสดงค่าจำนวนเต็มที่มากที่สุดซึ่งไม่มากกว่า number
ตัวอย่าง
| ตัวเลข | floor(number) |
|---|---|
| 20 | 20 |
| 10 | 10 |
| 0 | 0 |
| 2147483648 | 2147483648 |
| -0.4 | -1.0 |
| 0.4 | 0.0 |
| 22.5 | 22.0 |
+inf |
+inf |
-inf |
-inf |
Node.js
const result = await db.pipeline() .collection("books") .addFields( field("wordCount").divide(field("pages")).floor().as("wordsPerPage") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .addFields( field("wordCount").divide(field("pages")).floor().as("wordsPerPage") ) );
Swift
let result = try await db.pipeline() .collection("books") .addFields([ Field("wordCount").divide(Field("pages")).floor().as("wordsPerPage") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .addFields( Expression.divide(field("wordCount"), field("pages")).floor().alias("wordsPerPage") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .addFields( Expression.divide(field("wordCount"), field("pages")).floor().alias("wordsPerPage") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .add_fields( Field.of("wordCount").divide(Field.of("pages")).floor().as_("wordsPerPage") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .addFields(floor(divide(field("wordCount"), field("pages"))).as("wordsPerPage")) .execute() .get();
ROUND
ไวยากรณ์:
round[N <: INT32 | INT64 | FLOAT64 | DECIMAL128](number: N) -> N
round[N <: INT32 | INT64 | FLOAT64 | DECIMAL128](number: N, places: INT64) -> N
คำอธิบาย:
ปัดเศษ places หลักของ number ปัดเศษจากด้านขวาของจุดทศนิยมหาก places เป็นค่าบวก และปัดเศษจากด้านซ้ายของจุดทศนิยมหากเป็นค่าลบ
- หากระบุเฉพาะ
numberระบบจะปัดเศษเป็นค่าจำนวนเต็มที่ใกล้ที่สุด - ปัดเศษออกจาก 0 ในกรณีที่เป็นครึ่งหนึ่ง
- ระบบจะส่ง
errorหากการปัดเศษที่มีค่าplacesเป็นลบทำให้เกิดการล้น
ตัวอย่าง
| ตัวเลข | สถานที่ | round(number, places) |
|---|---|---|
| 15.5 | 0 | 16.0 |
| -15.5 | 0 | -16.0 |
| 15 | 1 | 15 |
| 15 | 0 | 15 |
| 15 | -1 | 20 |
| 15 | -2 | 0 |
| 15.48924 | 1 | 15.5 |
| 231-1 | -1 | [error] |
| 263-1L | -1 | [error] |
Node.js
const result = await db.pipeline() .collection("books") .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) .aggregate(field("partialRevenue").sum().as("totalRevenue")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) .aggregate(field("partialRevenue").sum().as("totalRevenue")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("soldBooks").multiply(Field("price")).round().as("partialRevenue")]) .aggregate([Field("partialRevenue").sum().as("totalRevenue")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(Expression.multiply(field("soldBooks"), field("price")).round().alias("partialRevenue")) .aggregate(AggregateFunction.sum("partialRevenue").alias("totalRevenue")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.multiply(field("soldBooks"), field("price")).round().alias("partialRevenue")) .aggregate(AggregateFunction.sum("partialRevenue").alias("totalRevenue")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("soldBooks") .multiply(Field.of("price")) .round() .as_("partialRevenue") ) .aggregate(Field.of("partialRevenue").sum().as_("totalRevenue")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(round(multiply(field("soldBooks"), field("price"))).as("partialRevenue")) .aggregate(sum("partialRevenue").as("totalRevenue")) .execute() .get();
TRUNC
ไวยากรณ์:
trunc[N <: Number](number: N) -> N
trunc[N <: Number](number: N, places: INT64) -> N
คำอธิบาย:
ตัดทอน number ให้เหลือจำนวนหลักทศนิยมที่ระบุ places ตัด
ตัวเลขจากด้านขวาของจุดทศนิยมหาก places เป็นบวก และตัดจากด้านซ้ายของจุดทศนิยมหากเป็นลบ
- หากระบุเฉพาะ
numberระบบจะตัดทอนเป็นค่าจำนวนเต็มที่ใกล้ที่สุดเข้าใกล้ศูนย์ - ระบบจะแสดง
errorหากการตัดทอนทำให้ผลลัพธ์เกิน
ตัวอย่าง
| ตัวเลข | สถานที่ | trunc(number, places) |
|---|---|---|
| 15.5 | 0 | 15.0 |
| -15.5 | 0 | -15.0 |
| 15 | 1 | 15 |
| 15 | 0 | 15 |
| 15 | -1 | 10 |
| 15 | -2 | 0 |
| 15.48924 | 1 | 15.4 |
| -15.48924 | 2 | -15.48 |
POW
ไวยากรณ์:
pow(base: FLOAT64, exponent: FLOAT64) -> FLOAT64
คำอธิบาย:
แสดงผลค่า base ยกกำลัง exponent
แสดงข้อผิดพลาดหาก
base <= 0และexponentเป็นค่าลบสำหรับ
exponentใดๆpow(1, exponent)จะเท่ากับ 1สำหรับ
baseใดๆpow(base, 0)จะเท่ากับ 1
ตัวอย่าง
| ฐาน | เลขชี้กำลัง | pow(base, exponent) |
|---|---|---|
| 2 | 3 | 8.0 |
| 2 | -3 | 0.125 |
+inf |
0 | 1.0 |
| 1 | +inf |
1.0 |
| -1 | 0.5 | [error] |
| 0 | -1 | [error] |
Node.js
const googleplex = { latitude: 37.4221, longitude: 122.0853 }; const result = await db.pipeline() .collection("cities") .addFields( field("lat").subtract(constant(googleplex.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("latitudeDifference"), field("lng").subtract(constant(googleplex.longitude)) .multiply(111 /* km per degree */) .pow(2) .as("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle") ) .execute();
Web
const googleplex = { latitude: 37.4221, longitude: 122.0853 }; const result = await execute(db.pipeline() .collection("cities") .addFields( field("lat").subtract(constant(googleplex.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("latitudeDifference"), field("lng").subtract(constant(googleplex.longitude)) .multiply(111 /* km per degree */) .pow(2) .as("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle") ) );
Swift
let googleplex = CLLocation(latitude: 37.4221, longitude: 122.0853) let result = try await db.pipeline() .collection("cities") .addFields([ Field("lat").subtract(Constant(googleplex.coordinate.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("latitudeDifference"), Field("lng").subtract(Constant(googleplex.coordinate.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("longitudeDifference") ]) .select([ Field("latitudeDifference").add(Field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle") ]) .execute()
Kotlin
val googleplex = GeoPoint(37.4221, -122.0853) val result = db.pipeline() .collection("cities") .addFields( field("lat").subtract(googleplex.latitude) .multiply(111 /* km per degree */) .pow(2) .alias("latitudeDifference"), field("lng").subtract(googleplex.longitude) .multiply(111 /* km per degree */) .pow(2) .alias("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .alias("approximateDistanceToGoogle") ) .execute()
Java
GeoPoint googleplex = new GeoPoint(37.4221, -122.0853); Task<Pipeline.Snapshot> result = db.pipeline() .collection("cities") .addFields( field("lat").subtract(googleplex.getLatitude()) .multiply(111 /* km per degree */) .pow(2) .alias("latitudeDifference"), field("lng").subtract(googleplex.getLongitude()) .multiply(111 /* km per degree */) .pow(2) .alias("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .alias("approximateDistanceToGoogle") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field googleplexLat = 37.4221 googleplexLng = -122.0853 result = ( client.pipeline() .collection("cities") .add_fields( Field.of("lat") .subtract(googleplexLat) .multiply(111) # km per degree .pow(2) .as_("latitudeDifference"), Field.of("lng") .subtract(googleplexLng) .multiply(111) # km per degree .pow(2) .as_("longitudeDifference"), ) .select( Field.of("latitudeDifference") .add(Field.of("longitudeDifference")) .sqrt() # Inaccurate for large distances or close to poles .as_("approximateDistanceToGoogle") ) .execute() )
Java
double googleplexLat = 37.4221; double googleplexLng = -122.0853; Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .addFields( pow(multiply(subtract(field("lat"), googleplexLat), 111), 2) .as("latitudeDifference"), pow(multiply(subtract(field("lng"), googleplexLng), 111), 2) .as("longitudeDifference")) .select( sqrt(add(field("latitudeDifference"), field("longitudeDifference"))) // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle")) .execute() .get();
SQRT
ไวยากรณ์:
sqrt[N <: FLOAT64 | DECIMAL128](number: N) -> N
คำอธิบาย:
แสดงผลรากที่สองของ number
- แสดง
errorหากnumberเป็นค่าลบ
ตัวอย่าง
| ตัวเลข | sqrt(number) |
|---|---|
| 25 | 5.0 |
| 12.002 | 3.464... |
| 0.0 | 0.0 |
NaN |
NaN |
+inf |
+inf |
-inf |
[error] |
x < 0 |
[error] |
Node.js
const googleplex = { latitude: 37.4221, longitude: 122.0853 }; const result = await db.pipeline() .collection("cities") .addFields( field("lat").subtract(constant(googleplex.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("latitudeDifference"), field("lng").subtract(constant(googleplex.longitude)) .multiply(111 /* km per degree */) .pow(2) .as("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle") ) .execute();
Web
const googleplex = { latitude: 37.4221, longitude: 122.0853 }; const result = await execute(db.pipeline() .collection("cities") .addFields( field("lat").subtract(constant(googleplex.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("latitudeDifference"), field("lng").subtract(constant(googleplex.longitude)) .multiply(111 /* km per degree */) .pow(2) .as("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle") ) );
Swift
let googleplex = CLLocation(latitude: 37.4221, longitude: 122.0853) let result = try await db.pipeline() .collection("cities") .addFields([ Field("lat").subtract(Constant(googleplex.coordinate.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("latitudeDifference"), Field("lng").subtract(Constant(googleplex.coordinate.latitude)) .multiply(111 /* km per degree */) .pow(2) .as("longitudeDifference") ]) .select([ Field("latitudeDifference").add(Field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle") ]) .execute()
Kotlin
val googleplex = GeoPoint(37.4221, -122.0853) val result = db.pipeline() .collection("cities") .addFields( field("lat").subtract(googleplex.latitude) .multiply(111 /* km per degree */) .pow(2) .alias("latitudeDifference"), field("lng").subtract(googleplex.longitude) .multiply(111 /* km per degree */) .pow(2) .alias("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .alias("approximateDistanceToGoogle") ) .execute()
Java
GeoPoint googleplex = new GeoPoint(37.4221, -122.0853); Task<Pipeline.Snapshot> result = db.pipeline() .collection("cities") .addFields( field("lat").subtract(googleplex.getLatitude()) .multiply(111 /* km per degree */) .pow(2) .alias("latitudeDifference"), field("lng").subtract(googleplex.getLongitude()) .multiply(111 /* km per degree */) .pow(2) .alias("longitudeDifference") ) .select( field("latitudeDifference").add(field("longitudeDifference")).sqrt() // Inaccurate for large distances or close to poles .alias("approximateDistanceToGoogle") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field googleplexLat = 37.4221 googleplexLng = -122.0853 result = ( client.pipeline() .collection("cities") .add_fields( Field.of("lat") .subtract(googleplexLat) .multiply(111) # km per degree .pow(2) .as_("latitudeDifference"), Field.of("lng") .subtract(googleplexLng) .multiply(111) # km per degree .pow(2) .as_("longitudeDifference"), ) .select( Field.of("latitudeDifference") .add(Field.of("longitudeDifference")) .sqrt() # Inaccurate for large distances or close to poles .as_("approximateDistanceToGoogle") ) .execute() )
Java
double googleplexLat = 37.4221; double googleplexLng = -122.0853; Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .addFields( pow(multiply(subtract(field("lat"), googleplexLat), 111), 2) .as("latitudeDifference"), pow(multiply(subtract(field("lng"), googleplexLng), 111), 2) .as("longitudeDifference")) .select( sqrt(add(field("latitudeDifference"), field("longitudeDifference"))) // Inaccurate for large distances or close to poles .as("approximateDistanceToGoogle")) .execute() .get();
ส่งออก
ไวยากรณ์:
exp(exponent: FLOAT64) -> FLOAT64
คำอธิบาย:
แสดงผลค่าของจำนวนออยเลอร์ที่ยกกำลังด้วย exponent หรือที่เรียกว่าฟังก์ชันเลขยกกำลังธรรมชาติ
ตัวอย่าง
| เลขชี้กำลัง | exp(exponent) |
|---|---|
| 0.0 | 1.0 |
| 10 | e^10 (FLOAT64) |
+inf |
+inf |
-inf |
0 |
Node.js
const result = await db.pipeline() .collection("books") .select(field("rating").exp().as("expRating")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("rating").exp().as("expRating")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("rating").exp().as("expRating")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("rating").exp().alias("expRating")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("rating").exp().alias("expRating")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").exp().as_("expRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(exp(field("rating")).as("expRating")) .execute() .get();
LN
ไวยากรณ์:
ln(number: FLOAT64) -> FLOAT64
คำอธิบาย:
แสดงผลลอการิทึมธรรมชาติของ number ฟังก์ชันนี้เทียบเท่ากับ log(number)
ตัวอย่าง
| ตัวเลข | ln(number) |
|---|---|
| 1 | 0.0 |
| 2L | 0.693... |
| 1.0 | 0.0 |
e (FLOAT64) |
1.0 |
-inf |
NaN |
+inf |
+inf |
x <= 0 |
[error] |
Node.js
const result = await db.pipeline() .collection("books") .select(field("rating").ln().as("lnRating")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("rating").ln().as("lnRating")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("rating").ln().as("lnRating")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("rating").ln().alias("lnRating")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("rating").ln().alias("lnRating")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").ln().as_("lnRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(ln(field("rating")).as("lnRating")) .execute() .get();
LOG
ไวยากรณ์:
log(number: FLOAT64, base: FLOAT64) -> FLOAT64
log(number: FLOAT64) -> FLOAT64
คำอธิบาย:
แสดงผลลอการิทึมของ number ถึง base
- หากระบุเฉพาะ
numberฟังก์ชันจะแสดงผลลอการิทึมของnumberฐานbase(เหมือนกับln(number))
ตัวอย่าง
| ตัวเลข | ฐาน | log(number, base) |
|---|---|---|
| 100 | 10 | 2.0 |
-inf |
Numeric |
NaN |
Numeric |
+inf |
NaN |
number <= 0 |
Numeric |
[error] |
Numeric |
base <= 0 |
[error] |
Numeric |
1.0 | [error] |
LOG10
ไวยากรณ์:
log10(x: FLOAT64) -> FLOAT64
คำอธิบาย:
แสดงผลค่าลอการิทึมของ number ในฐาน 10
ตัวอย่าง
| ตัวเลข | log10(number) |
|---|---|
| 100 | 2.0 |
-inf |
NaN |
+inf |
+inf |
x <= 0 |
[error] |
RAND
ไวยากรณ์:
rand() -> FLOAT64
คำอธิบาย:
แสดงผลจำนวนทศนิยมแบบสุ่มเทียมที่เลือกอย่างสม่ำเสมอระหว่าง 0.0 (รวม) และ 1.0 (ไม่รวม)
ฟังก์ชันอาร์เรย์
| ชื่อ | คำอธิบาย |
ARRAY
|
แสดง ARRAY ที่มีองค์ประกอบ 1 รายการสำหรับอาร์กิวเมนต์อินพุตแต่ละรายการ
|
ARRAY_CONCAT
|
เชื่อมต่ออาร์เรย์หลายรายการเป็นARRAYเดียว
|
ARRAY_CONTAINS
|
แสดงผล TRUE หาก ARRAY ที่ระบุมีค่าที่ต้องการ
|
ARRAY_CONTAINS_ALL
|
แสดงผล TRUE หากค่าทั้งหมดอยู่ใน ARRAY
|
ARRAY_CONTAINS_ANY
|
แสดงผล TRUE หากมีค่าใดค่าหนึ่งอยู่ใน ARRAY
|
ARRAY_FILTER
|
กรององค์ประกอบจาก ARRAY ที่ไม่ตรงตามเพรดิเคต
|
ARRAY_FIRST
|
แสดงผลองค์ประกอบแรกใน ARRAY
|
ARRAY_FIRST_N
|
แสดงองค์ประกอบ n แรกใน ARRAY
|
ARRAY_GET
|
แสดงผลองค์ประกอบที่ดัชนีที่ระบุใน ARRAY
|
ARRAY_INDEX_OF
|
แสดงดัชนีของการเกิดค่าครั้งแรกใน ARRAY
|
ARRAY_INDEX_OF_ALL
|
แสดงผลดัชนีทั้งหมดของค่าใน ARRAY
|
ARRAY_LENGTH
|
แสดงผลจำนวนองค์ประกอบใน ARRAY
|
ARRAY_LAST
|
แสดงผลองค์ประกอบสุดท้ายใน ARRAY
|
ARRAY_LAST_N
|
แสดงผลองค์ประกอบ n สุดท้ายใน ARRAY
|
ARRAY_REVERSE
|
กลับลำดับขององค์ประกอบใน ARRAY
|
ARRAY_SLICE
|
แสดงผลส่วนของ ARRAY
|
ARRAY_TRANSFORM
|
เปลี่ยนรูปแบบองค์ประกอบใน ARRAY โดยใช้นิพจน์กับแต่ละองค์ประกอบ
|
MAXIMUM
|
แสดงผลค่าสูงสุดใน ARRAY
|
MAXIMUM_N
|
แสดงผลnค่าที่ใหญ่ที่สุดในARRAY
|
MINIMUM
|
แสดงผลค่าต่ำสุดใน ARRAY
|
MINIMUM_N
|
แสดงผลnค่าที่เล็กที่สุดในARRAY
|
SUM
|
แสดงผลผลรวมของค่า NUMERIC ทั้งหมดใน ARRAY
|
JOIN
|
สร้างการเชื่อมต่อขององค์ประกอบใน ARRAY เป็นค่า STRING
|
ARRAY
ไวยากรณ์:
array(values: ANY...) -> ARRAY
คำอธิบาย:
สร้างอาร์เรย์จากองค์ประกอบที่ระบุ
- หากไม่มีอาร์กิวเมนต์ ระบบจะแทนที่ด้วย
NULLในอาร์เรย์ผลลัพธ์
ตัวอย่าง
| values | array(values) |
|---|---|
| () | [] |
| (1, 2, 3) | [1, 2, 3] |
| ("a", 1, true) | ["a", 1, true] |
| (1, null) | [1, null] |
| (1, [2, 3]) | [1, [2, 3]] |
ARRAY_CONCAT
ไวยากรณ์:
array_concat(arrays: ARRAY...) -> ARRAY
คำอธิบาย:
เชื่อมต่ออาร์เรย์ 2 รายการขึ้นไปเป็น ARRAY เดียว
ตัวอย่าง
| อาร์เรย์ | array_concat(arrays) |
|---|---|
| ([1, 2], [3, 4]) | [1, 2, 3, 4] |
| (["a", "b"], ["c"]) | ["a", "b", "c"] |
| ([1], [2], [3]) | [1, 2, 3] |
| ([], [1, 2]) | [1, 2] |
Node.js
const result = await db.pipeline() .collection("books") .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) .execute();
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayConcat([Field("subGenre")]).as("allGenres")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayConcat(field("subGenre")).alias("allGenres")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayConcat(field("subGenre")).alias("allGenres")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_concat(Field.of("subGenre")).as_("allGenres")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(arrayConcat(field("genre"), field("subGenre")).as("allGenres")) .execute() .get();
ARRAY_CONTAINS
ไวยากรณ์:
array_contains(array: ARRAY, value: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากพบ value ใน array และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
| อาร์เรย์ | value | array_contains(array, value) |
|---|---|---|
| [1, 2, 3] | 2 | จริง |
| [[1, 2], [3]] | [1, 2] | จริง |
| [1, null] | Null | จริง |
| "abc" | ทั้งหมด | ข้อผิดพลาด |
Node.js
const result = await db.pipeline() .collection("books") .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayContains(Constant("mystery")).as("isMystery")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayContains("mystery").alias("isMystery")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayContains("mystery").alias("isMystery")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_contains("mystery").as_("isMystery")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(arrayContains(field("genre"), "mystery").as("isMystery")) .execute() .get();
ARRAY_CONTAINS_ALL
ไวยากรณ์:
array_contains_all(array: ARRAY, search_values: ARRAY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากพบ search_values ทั้งหมดใน array และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
| อาร์เรย์ | search_values | array_contains_all(array, search_values) |
|---|---|---|
| [1, 2, 3] | [1, 2] | จริง |
| [1, 2, 3] | [1, 4] | เท็จ |
| [1, null] | [null] | จริง |
| [NaN] | [NaN] | จริง |
| [] | [] | จริง |
| [1, 2, 3] | [] | จริง |
Node.js
const result = await db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll([constant("fantasy"), constant("adventure")]) .as("isFantasyAdventure") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll([constant("fantasy"), constant("adventure")]) .as("isFantasyAdventure") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("genre") .arrayContainsAll([Constant("fantasy"), Constant("adventure")]) .as("isFantasyAdventure") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll(listOf("fantasy", "adventure")) .alias("isFantasyAdventure") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll(Arrays.asList("fantasy", "adventure")) .alias("isFantasyAdventure") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("genre") .array_contains_all(["fantasy", "adventure"]) .as_("isFantasyAdventure") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( arrayContainsAll(field("genre"), Arrays.asList("fantasy", "adventure")) .as("isFantasyAdventure")) .execute() .get();
ARRAY_CONTAINS_ANY
ไวยากรณ์:
array_contains_any(array: ARRAY, search_values: ARRAY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากพบ search_values ใน array และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
| อาร์เรย์ | search_values | array_contains_any(array, search_values) |
|---|---|---|
| [1, 2, 3] | [4, 1] | จริง |
| [1, 2, 3] | [4, 5] | เท็จ |
| [1, 2, null] | [null] | จริง |
Node.js
const result = await db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) .as("isMysteryOrFantasy") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) .as("isMysteryOrFantasy") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("genre") .arrayContainsAny([Constant("fantasy"), Constant("nonfiction")]) .as("isMysteryOrFantasy") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny(listOf("fantasy", "nonfiction")) .alias("isMysteryOrFantasy") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny(Arrays.asList("fantasy", "nonfiction")) .alias("isMysteryOrFantasy") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("genre") .array_contains_any(["fantasy", "nonfiction"]) .as_("isMysteryOrFantasy") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( arrayContainsAny(field("genre"), Arrays.asList("fantasy", "nonfiction")) .as("isMysteryOrFantasy")) .execute() .get();
ARRAY_FILTER
ไวยากรณ์:
array_filter(array: ARRAY, predicate: (ANY) -> BOOLEAN) -> ARRAY
คำอธิบาย:
กรอง array โดยใช้นิพจน์ predicate ซึ่งจะแสดงผลอาร์เรย์ใหม่ที่มีเฉพาะองค์ประกอบที่ตรงตามเพรดิเคต
- ระบบจะประเมิน
predicateสำหรับแต่ละองค์ประกอบในarrayหากแสดงผลtrueระบบจะรวมองค์ประกอบไว้ในผลลัพธ์ มิฉะนั้น (หากแสดงผลfalseหรือnull) ระบบจะไม่รวมองค์ประกอบ - หาก
predicateประเมินเป็นค่าที่ไม่ใช่บูลีนหรือค่าที่ไม่ใช่ Null ฟังก์ชันจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | เพรดิเคต | array_filter(array, predicate) |
|---|---|---|
| [1, 2, 3] | x -> x > 1 | [2, 3] |
| [1, null, 3] | x -> x > 1 | [3] |
| ["a", "b", "c"] | x -> x != "b" | ["a", "c"] |
| [] | x -> true | [] |
ARRAY_GET
ไวยากรณ์:
array_get(array: ARRAY, index: INT64) -> ANY
คำอธิบาย:
แสดงผลองค์ประกอบที่ดัชนี 0 ใน index ใน array
- หาก
indexเป็นค่าลบ ระบบจะเข้าถึงองค์ประกอบจากท้ายอาร์เรย์ โดย-1จะเป็นองค์ประกอบสุดท้าย - หาก
arrayไม่ใช่ประเภทARRAYและไม่ใช่nullจะแสดงผลข้อผิดพลาด - หาก
indexอยู่นอกขอบเขต ฟังก์ชันจะแสดงผลค่าที่ไม่มี - หาก
indexไม่ใช่ประเภทINT64ฟังก์ชันจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | ดัชนี | array_get(array, index) |
|---|---|---|
| [1, 2, 3] | 0 | 1 |
| [1, 2, 3] | -1 | 3 |
| [1, 2, 3] | 3 | ไม่อยู่ |
| [1, 2, 3] | -4 | ไม่อยู่ |
| "abc" | 0 | ข้อผิดพลาด |
| Null | 0 | Null |
Array |
"ก" | ข้อผิดพลาด |
Array |
2.0 | ข้อผิดพลาด |
ARRAY_LENGTH
ไวยากรณ์:
array_length(array: ARRAY) -> INT64
คำอธิบาย:
แสดงผลจำนวนองค์ประกอบใน array
ตัวอย่าง
| อาร์เรย์ | array_length(array) |
|---|---|
| [1, 2, 3] | 3 |
| [] | 0 |
| [1, 1, 1] | 3 |
| [1, null] | 2 |
Node.js
const result = await db.pipeline() .collection("books") .select(field("genre").arrayLength().as("genreCount")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayLength().as("genreCount")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayLength().as("genreCount")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayLength().alias("genreCount")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayLength().alias("genreCount")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_length().as_("genreCount")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(arrayLength(field("genre")).as("genreCount")) .execute() .get();
ARRAY_REVERSE
ไวยากรณ์:
array_reverse(array: ARRAY) -> ARRAY
คำอธิบาย:
กลับค่าของ array ที่ระบุ
ตัวอย่าง
| อาร์เรย์ | array_reverse(array) |
|---|---|
| [1, 2, 3] | [3, 2, 1] |
| ["a", "b"] | ["b", "a"] |
| [1, 2, 2, 3] | [3, 2, 2, 1] |
Node.js
const result = await db.pipeline() .collection("books") .select(arrayReverse(field("genre")).as("reversedGenres")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayReverse().as("reversedGenres")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayReverse().as("reversedGenres")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayReverse().alias("reversedGenres")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayReverse().alias("reversedGenres")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_reverse().as_("reversedGenres")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(arrayReverse(field("genre")).as("reversedGenres")) .execute() .get();
ARRAY_FIRST
ไวยากรณ์:
array_first(array: ARRAY) -> ANY
คำอธิบาย:
แสดงผลองค์ประกอบแรกใน array ซึ่งเทียบเท่ากับ array_get(array, 0)
- หาก
arrayว่างเปล่า จะแสดงผลค่าที่ไม่มี
ตัวอย่าง
| อาร์เรย์ | array_first(array) |
|---|---|
| [1, 2, 3] | 1 |
| [] | ไม่อยู่ |
ARRAY_FIRST_N
ไวยากรณ์:
array_first_n(array: ARRAY, n: INT64) -> ARRAY
คำอธิบาย:
แสดงองค์ประกอบ n แรกของ array ซึ่งเทียบเท่ากับ array_slice(array, 0, n)
- หาก
nเป็นค่าลบ ระบบจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | n | array_first_n(array, n) |
|---|---|---|
| [1, 2, 3, 4, 5] | 3 | [1, 2, 3] |
| [1, 2] | 3 | [1, 2] |
| [1, 2, 3] | 0 | [] |
ARRAY_INDEX_OF
ไวยากรณ์:
array_index_of(array: ARRAY, value: ANY) -> INT64
คำอธิบาย:
แสดงดัชนีแบบ 0 ของการเกิดครั้งแรกของ value ใน array แสดงผล -1 หากไม่พบ value
ตัวอย่าง
| อาร์เรย์ | value | array_index_of(array, value) |
|---|---|---|
| [1, 2, 3, 2] | 2 | 1 |
| [1, 2, 3] | 4 | -1 |
| [1, null, 3] | Null | 1 |
ARRAY_INDEX_OF_ALL
ไวยากรณ์:
array_index_of_all(array: ARRAY, value: ANY) -> ARRAY<INT64>
คำอธิบาย:
แสดงผลอาร์เรย์ที่มีดัชนีแบบ 0 ของการเกิด value ทั้งหมดใน array แสดงผล [] หากไม่พบ value
ตัวอย่าง
| อาร์เรย์ | value | array_index_of_all(array, value) |
|---|---|---|
| [1, 2, 3, 2] | 2 | [1, 3] |
| [1, 2, 3] | 4 | [] |
| [1, null, 3, null] | Null | [1, 3] |
ARRAY_LAST
ไวยากรณ์:
array_last(array: ARRAY) -> ANY
คำอธิบาย:
แสดงองค์ประกอบสุดท้ายใน array ซึ่งเทียบเท่ากับ array_get(array, -1)
- หาก
arrayว่างเปล่า จะแสดงผลค่าที่ไม่มี
ตัวอย่าง
| อาร์เรย์ | array_last(array) |
|---|---|
| [1, 2, 3] | 3 |
| [] | ไม่อยู่ |
ARRAY_LAST_N
ไวยากรณ์:
array_last_n(array: ARRAY, n: INT64) -> ARRAY
คำอธิบาย:
แสดงองค์ประกอบ n ตัวสุดท้ายของ array
- หาก
nเป็นค่าลบ ระบบจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | n | array_last_n(array, n) |
|---|---|---|
| [1, 2, 3, 4, 5] | 3 | [3, 4, 5] |
| [1, 2] | 3 | [1, 2] |
| [1, 2, 3] | 0 | [] |
ARRAY_SLICE
ไวยากรณ์:
array_slice(array: ARRAY, offset: INT64, length: INT64) -> ARRAY
คำอธิบาย:
แสดงผลชุดย่อยของ array โดยเริ่มจากดัชนีที่อิงตาม 0 offset และรวมองค์ประกอบ length
- หาก
offsetเป็นค่าลบ จะหมายถึงตำแหน่งเริ่มต้นจากท้ายอาร์เรย์ โดย-1จะเป็นองค์ประกอบสุดท้าย - หาก
lengthมากกว่าจำนวนองค์ประกอบที่เหลือในอาร์เรย์หลังจากoffsetผลลัพธ์จะขยายไปจนถึงท้ายอาร์เรย์ lengthต้องไม่เป็นค่าลบ มิฉะนั้นจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | หักลบ | ความยาว | array_slice(array, offset, length) |
|---|---|---|---|
| [1, 2, 3, 4, 5] | 1 | 3 | [2, 3, 4] |
| [1, 2, 3, 4, 5] | -2 | 2 | [4, 5] |
| [1, 2, 3] | 1 | 5 | [2, 3] |
| [1, 2, 3] | 3 | 2 | [] |
ARRAY_TRANSFORM
ไวยากรณ์:
array_transform(array: ARRAY, expression: (ANY) -> ANY) -> ARRAY
array_transform(array: ARRAY, expression: (ANY, INT64) -> ANY) -> ARRAY
คำอธิบาย:
เปลี่ยนรูปแบบ array โดยใช้ expression กับแต่ละองค์ประกอบ แล้วแสดงผลอาร์เรย์ใหม่ที่มีองค์ประกอบที่เปลี่ยนรูปแบบ อาร์เรย์เอาต์พุตจะมีขนาดเท่ากับอาร์เรย์อินพุตเสมอ
expressionอาจเป็นฟังก์ชันเอกภาคelement -> resultหรือฟังก์ชันทวิภาค(element, index) -> result- หาก
expressionเป็นฟังก์ชันแบบเอกภาค ระบบจะเรียกใช้ฟังก์ชันนี้กับแต่ละองค์ประกอบของarray - หาก
expressionเป็นไบนารี ระบบจะเรียกใช้โดยมีองค์ประกอบแต่ละรายการของarrayและดัชนีที่อิงตาม 0 ที่สอดคล้องกัน
ตัวอย่าง
| อาร์เรย์ | นิพจน์ | array_transform(array, expression) |
|---|---|---|
| [1, 2, 3] | x -> x * 2 | [2, 4, 6] |
| [1, 2, 3] | x -> x + 1 | [2, 3, 4] |
| [10, 20] | (x, i) -> x + i | [10, 21] |
| [] | x -> 1 | [] |
สูงสุด
ไวยากรณ์:
maximum(array: ARRAY) -> ANY
คำอธิบาย:
แสดงผลค่าสูงสุดใน array
- ระบบจะไม่สนใจค่า
NULLในระหว่างการเปรียบเทียบ - หาก
arrayว่างหรือมีเฉพาะค่าNULLจะแสดงผลNULL
ตัวอย่าง
| อาร์เรย์ | maximum(array) |
|---|---|
| [1, 5, 2] | 5 |
| [1, null, 5] | 5 |
| ["a", "c", "b"] | "ค" |
| [null, null] | Null |
| [] | Null |
MAXIMUM_N
ไวยากรณ์:
maximum_n(array: ARRAY, n: INT64) -> ARRAY
คำอธิบาย:
แสดงผลอาร์เรย์ของค่าที่ใหญ่ที่สุด n ใน array โดยเรียงจากมากไปน้อย
- ระบบจะไม่สนใจค่า
NULL - หาก
nเป็นค่าลบ ระบบจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | n | maximum_n(array, n) |
|---|---|---|
| [1, 5, 2, 4, 3] | 3 | [5, 4, 3] |
| [1, null, 5] | 3 | [5, 1] |
ขั้นต่ำ
ไวยากรณ์:
minimum(array: ARRAY) -> ANY
คำอธิบาย:
แสดงผลค่าต่ำสุดใน array
- ระบบจะไม่สนใจค่า
NULLในระหว่างการเปรียบเทียบ - หาก
arrayว่างหรือมีเฉพาะค่าNULLจะแสดงผลNULL
ตัวอย่าง
| อาร์เรย์ | minimum(array) |
|---|---|
| [1, 5, 2] | 1 |
| [5, null, 1] | 1 |
| ["a", "c", "b"] | "ก" |
| [null, null] | Null |
| [] | Null |
MINIMUM_N
ไวยากรณ์:
minimum_n(array: ARRAY, n: INT64) -> ARRAY
คำอธิบาย:
แสดงผลอาร์เรย์ของค่าที่เล็กที่สุด n ใน array ตามลำดับจากน้อยไปมาก
- ระบบจะไม่สนใจค่า
NULL - หาก
nเป็นค่าลบ ระบบจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | n | minimum_n(array, n) |
|---|---|---|
| [1, 5, 2, 4, 3] | 3 | [1, 2, 3] |
| [5, null, 1] | 3 | [1, 5] |
SUM
ไวยากรณ์:
sum(array: ARRAY) -> INT64 | FLOAT64
คำอธิบาย:
แสดงผลผลรวมของค่า NUMERIC ทั้งหมดใน ARRAY
- ระบบจะไม่สนใจค่าที่ไม่ใช่ตัวเลขในอาร์เรย์
- หากค่าตัวเลขใดในอาร์เรย์เป็น
NaNฟังก์ชันจะแสดงผลNaN - ระบบจะกำหนดประเภทการแสดงผลตามประเภทตัวเลขที่กว้างที่สุดในอาร์เรย์ ซึ่งก็คือ
INT64<FLOAT64 - หากเกิดจำนวนเต็ม 64 บิตล้นก่อนที่จะมีการรวมค่าทศนิยม ระบบจะแสดงข้อผิดพลาด หากมีการรวมค่าทศนิยม การล้นจะส่งผลให้เกิด +/- อนันต์
- หากอาร์เรย์ไม่มีค่าตัวเลขเลย ฟังก์ชันจะแสดงผล
NULL
ตัวอย่าง
| อาร์เรย์ | sum(array) |
|---|---|
| [1, 2, 3] | 6L |
| [1L, 2L, 3L] | 6L |
| [2000000000, 2000000000] | 4000000000L |
| [10, 20.5] | 30.5 |
| [1, "a", 2] | 3L |
| [INT64.MAX_VALUE, 1] | ข้อผิดพลาด |
| [INT64.MAX_VALUE, 1, -1.0] | ข้อผิดพลาด |
| [INT64.MAX_VALUE, 1.0] | 9.223372036854776e+18 |
เข้าร่วม
ไวยากรณ์:
join[T <: STRING | BYTES](array: ARRAY<T>, delimiter: T) -> STRING
join[T <: STRING | BYTES](array: ARRAY<T>, delimiter: T, null_text: T) -> STRING
คำอธิบาย:
แสดงผลการเชื่อมองค์ประกอบใน array เป็น STRING array อาจเป็นข้อมูลประเภท STRING หรือ BYTES
- องค์ประกอบทั้งหมดใน
array,delimiterและnull_textต้องเป็นประเภทเดียวกัน โดยต้องเป็นSTRINGทั้งหมดหรือเป็นBYTESทั้งหมด - หากระบุ
null_textระบบจะแทนที่ค่าNULLทั้งหมดในarrayด้วยnull_text - หากไม่ได้ระบุ
null_textระบบจะไม่รวมค่าNULLในarrayไว้ในผลลัพธ์
ตัวอย่าง
เมื่อไม่ได้ระบุ null_text
| อาร์เรย์ | ตัวคั่น | join(array, delimiter) |
|---|---|---|
| ["a", "b", "c"] | "," | "a,b,c" |
| ["a", null, "c"] | "," | "a,c" |
| [b'a', b'b', b'c'] | b',' | b'a,b,c' |
| ["a", b'c'] | "," | ข้อผิดพลาด |
| ["a", "c"] | b',' | ข้อผิดพลาด |
| [b'a', b'c'] | "," | ข้อผิดพลาด |
เมื่อมีการระบุnull_text
| อาร์เรย์ | ตัวคั่น | null_text | join(array, delimiter, null_text) |
|---|---|---|---|
| ["a", null, "c"] | "," | "MISSING" | "a,MISSING,c" |
| [b'a', null, b'c'] | b',' | b'NULL' | b'a,NULL,c' |
| [null, "b", null] | "," | "MISSING" | "MISSING,b,MISSING" |
| [b'a', null, null] | b',' | b'NULL' | b'a,NULL,NULL' |
| ["a", null] | "," | b'N' | ข้อผิดพลาด |
| [b'a', null] | b',' | "N" | ข้อผิดพลาด |
ฟังก์ชันการเปรียบเทียบ
| ชื่อ | คำอธิบาย |
EQUAL
|
การเปรียบเทียบความเท่ากัน |
GREATER_THAN
|
การเปรียบเทียบมากกว่า |
GREATER_THAN_OR_EQUAL
|
การเปรียบเทียบมากกว่าหรือเท่ากับ |
LESS_THAN
|
การเปรียบเทียบน้อยกว่า |
LESS_THAN_OR_EQUAL
|
การเปรียบเทียบน้อยกว่าหรือเท่ากับ |
NOT_EQUAL
|
การเปรียบเทียบ "ไม่เท่ากับ" |
CMP
|
การเปรียบเทียบทั่วไป |
เท่ากับ
ไวยากรณ์:
equal(x: ANY, y: ANY) -> BOOLEAN
ตัวอย่าง
x |
y |
equal(x, y) |
|---|---|---|
| 1L | 1L | TRUE |
| 1.0 | 1L | TRUE |
| -1.0 | 1L | FALSE |
| NaN | NaN | TRUE |
NULL |
NULL |
TRUE |
NULL |
ABSENT |
FALSE |
คำอธิบาย:
แสดงผล TRUE หาก x และ y เท่ากัน และ FALSE ในกรณีอื่นๆ
Node.js
const result = await db.pipeline() .collection("books") .select(field("rating").equal(5).as("hasPerfectRating")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("rating").equal(5).as("hasPerfectRating")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("rating").equal(5).as("hasPerfectRating")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("rating").equal(5).alias("hasPerfectRating")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("rating").equal(5).alias("hasPerfectRating")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").equal(5).as_("hasPerfectRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(equal(field("rating"), 5).as("hasPerfectRating")) .execute() .get();
มากกว่า
ไวยากรณ์:
greater_than(x: ANY, y: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก x มากกว่า y และแสดงผล FALSE ในกรณีอื่นๆ
หาก x และ y เปรียบเทียบกันไม่ได้ ฟังก์ชันจะแสดงผล FALSE
ตัวอย่าง
x |
y |
greater_than(x, y) |
|---|---|---|
| 1L | 0.0 | TRUE |
| 1L | 1L | FALSE |
| 1L | 2L | FALSE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
FALSE |
Node.js
const result = await db.pipeline() .collection("books") .select(field("rating").greaterThan(4).as("hasHighRating")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("rating").greaterThan(4).as("hasHighRating")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("rating").greaterThan(4).as("hasHighRating")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("rating").greaterThan(4).alias("hasHighRating")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("rating").greaterThan(4).alias("hasHighRating")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").greater_than(4).as_("hasHighRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(greaterThan(field("rating"), 4).as("hasHighRating")) .execute() .get();
มากกว่าหรือเท่ากับ
ไวยากรณ์:
greater_than_or_equal(x: ANY, y: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก x มากกว่าหรือเท่ากับ y และแสดงผล FALSE ในกรณีอื่นๆ
หาก x และ y เปรียบเทียบกันไม่ได้ ฟังก์ชันจะแสดงผล FALSE
ตัวอย่าง
x |
y |
greater_than_or_equal(x, y) |
|---|---|---|
| 1L | 0.0 | TRUE |
| 1L | 1L | TRUE |
| 1L | 2L | FALSE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
TRUE |
Node.js
const result = await db.pipeline() .collection("books") .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("published").greaterThanOrEqual(1900).alias("publishedIn20thCentury")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("published").greaterThanOrEqual(1900).alias("publishedIn20thCentury")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("published") .greater_than_or_equal(1900) .as_("publishedIn20thCentury") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(greaterThanOrEqual(field("published"), 1900).as("publishedIn20thCentury")) .execute() .get();
LESS_THAN
ไวยากรณ์:
less_than(x: ANY, y: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก x น้อยกว่า y และแสดงผล FALSE ในกรณีอื่นๆ
หาก x และ y เปรียบเทียบกันไม่ได้ ฟังก์ชันจะแสดงผล FALSE
ตัวอย่าง
x |
y |
less_than(x, y) |
|---|---|---|
| 1L | 0.0 | FALSE |
| 1L | 1L | FALSE |
| 1L | 2L | TRUE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
FALSE |
Node.js
const result = await db.pipeline() .collection("books") .select(field("published").lessThan(1923).as("isPublicDomainProbably")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("published").lessThan(1923).as("isPublicDomainProbably")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("published").lessThan(1923).as("isPublicDomainProbably")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("published").lessThan(1923).alias("isPublicDomainProbably")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("published").lessThan(1923).alias("isPublicDomainProbably")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("published").less_than(1923).as_("isPublicDomainProbably")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(lessThan(field("published"), 1923).as("isPublicDomainProbably")) .execute() .get();
LESS_THAN_OR_EQUAL
ไวยากรณ์:
less_than_or_equal(x: ANY, y: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก x น้อยกว่าหรือเท่ากับ y และแสดงผล FALSE หากไม่เป็นเช่นนั้น
หาก x และ y เปรียบเทียบกันไม่ได้ ฟังก์ชันจะแสดงผล FALSE
ตัวอย่าง
x |
y |
less_than(x, y) |
|---|---|---|
| 1L | 0.0 | FALSE |
| 1L | 1L | TRUE |
| 1L | 2L | TRUE |
| "foo" | 0L | FALSE |
| 0L | "foo" | FALSE |
| NaN | 0L | FALSE |
| 0L | NaN | FALSE |
NULL |
NULL |
TRUE |
Node.js
const result = await db.pipeline() .collection("books") .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("rating").lessThanOrEqual(2).as("hasBadRating")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("rating").lessThanOrEqual(2).alias("hasBadRating")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("rating").lessThanOrEqual(2).alias("hasBadRating")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").less_than_or_equal(2).as_("hasBadRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(lessThanOrEqual(field("rating"), 2).as("hasBadRating")) .execute() .get();
NOT_EQUAL
ไวยากรณ์:
not_equal(x: ANY, y: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก x ไม่เท่ากับ y และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
x |
y |
not_equal(x, y) |
|---|---|---|
| 1L | 1L | FALSE |
| 1.0 | 1L | FALSE |
| -1.0 | 1L | TRUE |
| NaN | 0L | TRUE |
| NaN | NaN | FALSE |
NULL |
NULL |
FALSE |
NULL |
ABSENT |
TRUE |
Node.js
const result = await db.pipeline() .collection("books") .select(field("title").notEqual("1984").as("not1984")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("title").notEqual("1984").as("not1984")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("title").notEqual("1984").as("not1984")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("title").notEqual("1984").alias("not1984")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("title").notEqual("1984").alias("not1984")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("title").not_equal("1984").as_("not1984")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(notEqual(field("title"), "1984").as("not1984")) .execute() .get();
CMP
ไวยากรณ์:
cmp(x: ANY, y: ANY) -> Int64
คำอธิบาย:
เปรียบเทียบ x กับ y โดยมีผู้ชมที่กลับมาดังนี้
1Lหากxมากกว่าy-1Lหากxน้อยกว่าy0Lมิฉะนั้น
ฟังก์ชัน cmp(...) จะทำงานในประเภทต่างๆ ตามลำดับเดียวกันกับที่ใช้ในขั้นตอน sort(...) ซึ่งต่างจากฟังก์ชันการเปรียบเทียบอื่นๆ ดูลำดับประเภทค่าเพื่อดูวิธีเรียงลำดับค่าในประเภทต่างๆ
ตัวอย่าง
x |
y |
cmp(x, y) |
|---|---|---|
| 1L | 1L | 0L |
| 1.0 | 1L | 0L |
| -1.0 | 1L | -1L |
| 42.5D | "foo" | -1L |
NULL |
NULL |
0L |
NULL |
ABSENT |
0L |
ฟังก์ชันการแก้ไขข้อบกพร่อง
| ชื่อ | คำอธิบาย |
EXISTS
|
แสดงผล TRUE หากค่าไม่ใช่ค่าที่ไม่มี
|
IS_ABSENT
|
แสดงผล TRUE หากค่าเป็นค่าที่ไม่มี
|
IF_ABSENT
|
แทนที่ค่าด้วยนิพจน์หากไม่มีค่า |
IS_ERROR
|
ดักจับและตรวจสอบว่านิพจน์พื้นฐานส่งข้อผิดพลาดหรือไม่ |
IF_ERROR
|
แทนที่ค่าด้วยนิพจน์หากเกิดข้อผิดพลาด |
ERROR
|
สิ้นสุดการประเมินและแสดงข้อผิดพลาดพร้อมข้อความที่ระบุ |
EXISTS
ไวยากรณ์:
exists(value: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value ไม่ใช่ค่าที่ไม่มี
ตัวอย่าง
value |
exists(value) |
|---|---|
| 0L | TRUE |
| "foo" | TRUE |
NULL |
TRUE |
ABSENT |
FALSE |
Node.js
const result = await db.pipeline() .collection("books") .select(field("rating").exists().as("hasRating")) .execute();
Web
ตัวอย่าง
const result = await execute(db.pipeline() .collection("books") .select(field("rating").exists().as("hasRating")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("rating").exists().as("hasRating")]) .execute()
Kotlin
ตัวอย่าง
val result = db.pipeline() .collection("books") .select(field("rating").exists().alias("hasRating")) .execute()
Java
ตัวอย่าง
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("rating").exists().alias("hasRating")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").exists().as_("hasRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(exists(field("rating")).as("hasRating")) .execute() .get();
IS_ABSENT
ไวยากรณ์:
is_absent(value: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value เป็นค่าที่ไม่มี และแสดงผล FALSE ในกรณีอื่นๆ ค่าที่ไม่มี
คือค่าที่ขาดหายไปจากอินพุต เช่น ฟิลด์เอกสารที่ขาดหายไป
ตัวอย่าง
value |
is_absent(value) |
|---|---|
| 0L | FALSE |
| "foo" | FALSE |
NULL |
FALSE |
ABSENT |
TRUE |
IF_ABSENT
ไวยากรณ์:
if_absent(value: ANY, replacement: ANY) -> ANY
คำอธิบาย:
หาก value เป็นค่าที่ไม่มีอยู่ ฟังก์ชันจะประเมินและแสดงผล replacement ไม่เช่นนั้นจะแสดงผล value
ตัวอย่าง
value |
replacement |
if_absent(value, replacement) |
|---|---|---|
| 5L | 0L | 5L |
NULL |
0L | NULL |
ABSENT |
0L | 0L |
IS_ERROR
ไวยากรณ์:
is_error(try: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากเกิดข้อผิดพลาดระหว่างการประเมิน try ไม่เช่นนั้นจะแสดงผลเป็น FALSE
IF_ERROR
ไวยากรณ์:
if_error(try: ANY, catch: ANY) -> ANY
คำอธิบาย:
หากเกิดข้อผิดพลาดระหว่างการประเมิน try ฟังก์ชันจะประเมินและแสดงผล replacement ไม่เช่นนั้นจะแสดงค่าที่แก้ไขแล้วของ try
ข้อผิดพลาด
ไวยากรณ์:
error(message: STRING) -> ANY
คำอธิบาย:
การประเมินฟังก์ชัน error จะส่งผลให้การประเมินไปป์ไลน์สิ้นสุดลงพร้อมข้อผิดพลาด message ที่ระบุจะรวมอยู่ในข้อผิดพลาด
ตัวอย่าง
cond |
res |
switch_on(cond, res, error("no condition matched")) |
|---|---|---|
TRUE |
1L | 1L |
FALSE |
1L | ERROR ("no condition matched") |
ฟังก์ชันอ้างอิง
REFERENCE ประเภทนี้ทำหน้าที่เป็น "ตัวชี้" ไปยังเอกสารอื่นๆ ในฐานข้อมูล (หรือแม้แต่ฐานข้อมูลอื่นๆ) ฟังก์ชันต่อไปนี้ช่วยให้คุณจัดการประเภทนี้
ในระหว่างการดำเนินการค้นหาได้
| ชื่อ | คำอธิบาย |
COLLECTION_ID
|
แสดงผลรหัสของคอลเล็กชันใบในการอ้างอิงที่กำหนด |
DOCUMENT_ID
|
แสดงผลรหัสของเอกสารในการอ้างอิงที่กำหนด |
PARENT
|
แสดงผลการอ้างอิงระดับบนสุด |
REFERENCE_SLICE
|
แสดงผลชุดย่อยของกลุ่มจากการอ้างอิงที่ระบุ |
COLLECTION_ID
ไวยากรณ์:
collection_id(ref: REFERENCE) -> STRING
คำอธิบาย:
แสดงผลรหัสคอลเล็กชันย่อยของ REFERENCE ที่ระบุ
ตัวอย่าง
ref |
collection_id(ref) |
|---|---|
users/user1 |
"users" |
users/user1/posts/post1 |
"posts" |
DOCUMENT_ID
ไวยากรณ์:
document_id(ref: REFERENCE) -> ANY
คำอธิบาย:
แสดงรหัสเอกสารของ REFERENCE ที่ระบุ
ตัวอย่าง
ref |
document_id(ref) |
|---|---|
users/user1 |
"user1" |
users/user1/posts/post1 |
"post1" |
ผู้ปกครอง
ไวยากรณ์:
parent(ref: REFERENCE) -> REFERENCE
คำอธิบาย:
แสดงผล REFERENCE ระดับบนสุดของการอ้างอิงที่ระบุ หรือ NULL หากการอ้างอิงเป็น
การอ้างอิงรูทอยู่แล้ว
ตัวอย่าง
ref |
parent(ref) |
|---|---|
/ |
NULL |
users/user1 |
/ |
users/user1/posts/post1 |
users/user1 |
REFERENCE_SLICE
ไวยากรณ์:
reference_slice(ref: REFERENCE, offset: INT, length: INT) -> REFERENCE
คำอธิบาย:
REFERENCE คือรายการของ(collection_id, document_id)ทูเพิล ซึ่งช่วยให้คุณ
ดูรายการดังกล่าวได้เหมือนกับ array_slice(...)
แสดงผล REFERENCE ใหม่ซึ่งเป็นส่วนย่อยของกลุ่มของ ref ที่ระบุ
offset: ดัชนีเริ่มต้น (อิงตาม 0) ของชิ้น หากเป็นค่าลบ จะเป็นออฟเซ็ตจากจุดสิ้นสุดของข้อมูลอ้างอิงlength: จำนวนกลุ่มที่จะรวมไว้ในชิ้น
ตัวอย่าง
ref |
offset |
length |
reference_slice(ref, offset, length) |
|---|---|---|---|
a/1/b/2/c/3 |
1L | 2L | b/2/c/3 |
a/1/b/2/c/3 |
0L | 2L | a/1/b/2 |
a/1/b/2/c/3 |
-2L | 2L | c/3 |
ฟังก์ชันตรรกะ
| ชื่อ | คำอธิบาย |
AND
|
ดำเนินการ AND เชิงตรรกะ |
OR
|
ดำเนินการ OR เชิงตรรกะ |
XOR
|
ดำเนินการ XOR เชิงตรรกะ |
NOT
|
ดำเนินการ NOT เชิงตรรกะ |
NOR
|
ดำเนินการ NOR เชิงตรรกะ |
CONDITIONAL
|
การประเมินสาขาตามนิพจน์แบบมีเงื่อนไข |
IF_NULL
|
แสดงค่าแรกที่ไม่ใช่ค่า Null |
SWITCH_ON
|
การประเมินสาขาตามชุดเงื่อนไข |
EQUAL_ANY
|
ตรวจสอบว่าค่าเท่ากับองค์ประกอบใดๆ ในอาร์เรย์หรือไม่ |
NOT_EQUAL_ANY
|
ตรวจสอบว่าค่าไม่เท่ากับองค์ประกอบใดๆ ในอาร์เรย์ |
MAXIMUM
|
แสดงผลค่าสูงสุดในชุดค่า |
MINIMUM
|
แสดงผลค่าต่ำสุดในชุดค่า |
และ
ไวยากรณ์:
and(x: BOOLEAN...) -> BOOLEAN
คำอธิบาย:
แสดงผล AND เชิงตรรกะของค่าบูลีนตั้งแต่ 2 ค่าขึ้นไป
แสดงผล NULL หากไม่สามารถหาผลลัพธ์ได้เนื่องจากค่าที่ระบุเป็น ABSENT หรือ NULL
ตัวอย่าง
x |
y |
and(x, y) |
|---|---|---|
TRUE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
NULL |
TRUE |
NULL |
ABSENT |
TRUE |
NULL |
NULL |
FALSE |
FALSE |
FALSE |
ABSENT |
FALSE |
Node.js
const result = await db.pipeline() .collection("books") .select( and(field("rating").greaterThan(4), field("price").lessThan(10)) .as("under10Recommendation") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( and(field("rating").greaterThan(4), field("price").lessThan(10)) .as("under10Recommendation") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ (Field("rating").greaterThan(4) && Field("price").lessThan(10)) .as("under10Recommendation") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( Expression.and(field("rating").greaterThan(4), field("price").lessThan(10)) .alias("under10Recommendation") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( Expression.and( field("rating").greaterThan(4), field("price").lessThan(10) ).alias("under10Recommendation") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field, And result = ( client.pipeline() .collection("books") .select( And( Field.of("rating").greater_than(4), Field.of("price").less_than(10) ).as_("under10Recommendation") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( and(greaterThan(field("rating"), 4), lessThan(field("price"), 10)) .as("under10Recommendation")) .execute() .get();
หรือ
ไวยากรณ์:
or(x: BOOLEAN...) -> BOOLEAN
คำอธิบาย:
แสดงผล OR เชิงตรรกะของค่าบูลีนตั้งแต่ 2 ค่าขึ้นไป
แสดงผล NULL หากไม่สามารถหาผลลัพธ์ได้เนื่องจากค่าที่ระบุเป็น ABSENT หรือ NULL
ตัวอย่าง
x |
y |
or(x, y) |
|---|---|---|
TRUE |
TRUE |
TRUE |
FALSE |
TRUE |
TRUE |
NULL |
TRUE |
TRUE |
ABSENT |
TRUE |
TRUE |
NULL |
FALSE |
NULL |
FALSE |
ABSENT |
NULL |
Node.js
const result = await db.pipeline() .collection("books") .select( or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) .as("matchesSearchFilters") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) .as("matchesSearchFilters") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ (Field("genre").equal("Fantasy") || Field("tags").arrayContains("adventure")) .as("matchesSearchFilters") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( Expression.or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) .alias("matchesSearchFilters") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( Expression.or( field("genre").equal("Fantasy"), field("tags").arrayContains("adventure") ).alias("matchesSearchFilters") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field, And, Or result = ( client.pipeline() .collection("books") .select( Or( Field.of("genre").equal("Fantasy"), Field.of("tags").array_contains("adventure"), ).as_("matchesSearchFilters") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( or(equal(field("genre"), "Fantasy"), arrayContains(field("tags"), "adventure")) .as("matchesSearchFilters")) .execute() .get();
XOR
ไวยากรณ์:
xor(x: BOOLEAN...) -> BOOLEAN
คำอธิบาย:
แสดงผล XOR เชิงตรรกะของค่าบูลีน 2 ค่าขึ้นไป
แสดงผล NULL หากค่าที่ระบุเป็น ABSENT หรือ NULL
ตัวอย่าง
x |
y |
xor(x, y) |
|---|---|---|
TRUE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
TRUE |
NULL |
TRUE |
NULL |
ABSENT |
TRUE |
NULL |
NULL |
FALSE |
NULL |
FALSE |
ABSENT |
NULL |
Node.js
const result = await execute(db.pipeline() .collection("books") .select( xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) .as("matchesSearchFilters") ) );
Web
const result = await execute(db.pipeline() .collection("books") .select( xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) .as("matchesSearchFilters") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ (Field("tags").arrayContains("magic") ^ Field("tags").arrayContains("nonfiction")) .as("matchesSearchFilters") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( Expression.xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) .alias("matchesSearchFilters") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( Expression.xor( field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction") ).alias("matchesSearchFilters") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field, Xor result = ( client.pipeline() .collection("books") .select( Xor( [ Field.of("tags").array_contains("magic"), Field.of("tags").array_contains("nonfiction"), ] ).as_("matchesSearchFilters") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( xor( arrayContains(field("tags"), "magic"), arrayContains(field("tags"), "nonfiction")) .as("matchesSearchFilters")) .execute() .get();
NOR
ไวยากรณ์:
nor(x: BOOLEAN...) -> BOOLEAN
คำอธิบาย:
แสดงผล NOR เชิงตรรกะของค่าบูลีนตั้งแต่ 2 ค่าขึ้นไป
แสดงผล NULL หากไม่สามารถหาผลลัพธ์ได้เนื่องจากค่าที่ระบุเป็น ABSENT หรือ NULL
ตัวอย่าง
x |
y |
nor(x, y) |
|---|---|---|
TRUE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
NULL |
TRUE |
FALSE |
ABSENT |
TRUE |
FALSE |
NULL |
FALSE |
NULL |
FALSE |
ABSENT |
NULL |
NOT
ไวยากรณ์:
not(x: BOOLEAN) -> BOOLEAN
คำอธิบาย:
แสดงผลค่า NOT เชิงตรรกะของค่าบูลีน
Node.js
const result = await execute(db.pipeline() .collection("books") .select( field("tags").arrayContains("nonfiction").not() .as("isFiction") ) );
Web
const result = await execute(db.pipeline() .collection("books") .select( field("tags").arrayContains("nonfiction").not() .as("isFiction") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ (!Field("tags").arrayContains("nonfiction")) .as("isFiction") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( Expression.not( field("tags").arrayContains("nonfiction") ).alias("isFiction") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( Expression.not( field("tags").arrayContains("nonfiction") ).alias("isFiction") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field, Not result = ( client.pipeline() .collection("books") .select(Not(Field.of("tags").array_contains("nonfiction")).as_("isFiction")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(not(arrayContains(field("tags"), "nonfiction")).as("isFiction")) .execute() .get();
CONDITIONAL
ไวยากรณ์:
conditional(condition: BOOLEAN, true_case: ANY, false_case: ANY) -> ANY
คำอธิบาย:
ประเมินและแสดงผล true_case หาก condition ประเมินเป็น TRUE
ประเมินและแสดงผล false_case หากเงื่อนไขเป็น FALSE, NULL หรือค่า ABSENT
ตัวอย่าง
condition |
true_case |
false_case |
conditional(condition, true_case, false_case) |
|---|---|---|---|
TRUE |
1L | 0L | 1L |
FALSE |
1L | 0L | 0L |
NULL |
1L | 0L | 0L |
ABSENT |
1L | 0L | 0L |
Node.js
const result = await execute(db.pipeline() .collection("books") .select( field("tags").arrayConcat([ field("pages").greaterThan(100) .conditional(constant("longRead"), constant("shortRead")) ]).as("extendedTags") ) );
Web
const result = await execute(db.pipeline() .collection("books") .select( field("tags").arrayConcat([ field("pages").greaterThan(100) .conditional(constant("longRead"), constant("shortRead")) ]).as("extendedTags") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("tags").arrayConcat([ ConditionalExpression( Field("pages").greaterThan(100), then: Constant("longRead"), else: Constant("shortRead") ) ]).as("extendedTags") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("tags").arrayConcat( Expression.conditional( field("pages").greaterThan(100), constant("longRead"), constant("shortRead") ) ).alias("extendedTags") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("tags").arrayConcat( Expression.conditional( field("pages").greaterThan(100), constant("longRead"), constant("shortRead") ) ).alias("extendedTags") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import ( Field, Constant, Conditional, ) result = ( client.pipeline() .collection("books") .select( Field.of("tags") .array_concat( Conditional( Field.of("pages").greater_than(100), Constant.of("longRead"), Constant.of("shortRead"), ) ) .as_("extendedTags") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( arrayConcat( field("tags"), conditional( greaterThan(field("pages"), 100), constant("longRead"), constant("shortRead"))) .as("extendedTags")) .execute() .get();
IF_NULL
ไวยากรณ์:
if_null(expr: ANY, replacement: ANY) -> ANY
คำอธิบาย:
แสดงผล expr หากไม่ใช่ NULL หรือมิฉะนั้นจะประเมินและแสดงผล
replacement ระบบจะไม่ประมวลผลนิพจน์ replacement หากใช้ expr
ตัวอย่าง
expr |
replacement |
if_null(expr, replacement) |
|---|---|---|
| 1L | 2L | 1L |
NULL |
2L | 2L |
ABSENT |
2L | ABSENT |
เปิด
ไวยากรณ์:
switch_on(cond1: BOOLEAN, res1: ANY, cond2: BOOLEAN, res2: ANY, ..., [default: ANY]) -> ANY
คำอธิบาย:
ประเมินชุดเงื่อนไขและแสดงผลลัพธ์ที่เชื่อมโยงกับเงื่อนไขTRUEแรก หากไม่มีเงื่อนไขใดประเมินเป็น TRUE ระบบจะแสดงค่า default
หากมีการระบุ หากไม่ได้ระบุค่า default ระบบจะแสดงข้อผิดพลาด
หากไม่มีเงื่อนไขอื่นที่ประเมินแล้วได้ค่า TRUE
หากต้องการระบุค่า default ให้ส่งเป็นอาร์กิวเมนต์สุดท้ายเพื่อให้มีอาร์กิวเมนต์เป็นจำนวนคี่
ตัวอย่าง
x |
switch_on(eq(x, 1L), "one", eq(x, 2L), "two", "other") |
|---|---|
| 1L | "หนึ่ง" |
| 2L | "สอง" |
| 3L | "อื่นๆ" |
EQUALS_ANY
ไวยากรณ์:
equal_any(value: ANY, search_space: ARRAY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value อยู่ในอาร์เรย์ search_space
ตัวอย่าง
value |
search_space |
equal_any(value, search_space) |
|---|---|---|
| 0L | [1L, 2L, 3L] | FALSE |
| 2L | [1L, 2L, 3L] | TRUE |
NULL |
[1L, 2L, 3L] | FALSE |
NULL |
[1L, NULL] |
TRUE |
ABSENT |
[1L, NULL] |
FALSE |
| NaN | [1L, NaN, 3L] | TRUE |
Node.js
const result = await execute(db.pipeline() .collection("books") .select( field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) .as("matchesGenreFilters") ) );
Web
const result = await execute(db.pipeline() .collection("books") .select( field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) .as("matchesGenreFilters") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) .as("matchesGenreFilters") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("genre").equalAny(listOf("Science Fiction", "Psychological Thriller")) .alias("matchesGenreFilters") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("genre").equalAny(Arrays.asList("Science Fiction", "Psychological Thriller")) .alias("matchesGenreFilters") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("genre") .equal_any(["Science Fiction", "Psychological Thriller"]) .as_("matchesGenreFilters") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( equalAny(field("genre"), Arrays.asList("Science Fiction", "Psychological Thriller")) .as("matchesGenreFilters")) .execute() .get();
NOT_EQUAL_ANY
ไวยากรณ์:
not_equal_any(value: ANY, search_space: ARRAY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value ไม่อยู่ในอาร์เรย์ search_space
ตัวอย่าง
value |
search_space |
not_equal_any(value, search_space) |
|---|---|---|
| 0L | [1L, 2L, 3L] | TRUE |
| 2L | [1L, 2L, 3L] | FALSE |
NULL |
[1L, 2L, 3L] | TRUE |
NULL |
[1L, NULL] |
FALSE |
ABSENT |
[1L, NULL] |
TRUE |
| NaN | [1L, NaN, 3L] | FALSE |
Node.js
const result = await execute(db.pipeline() .collection("books") .select( field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) .as("byExcludedAuthors") ) );
Web
const result = await execute(db.pipeline() .collection("books") .select( field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) .as("byExcludedAuthors") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) .as("byExcludedAuthors") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("author").notEqualAny(listOf("George Orwell", "F. Scott Fitzgerald")) .alias("byExcludedAuthors") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("author").notEqualAny(Arrays.asList("George Orwell", "F. Scott Fitzgerald")) .alias("byExcludedAuthors") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("author") .not_equal_any(["George Orwell", "F. Scott Fitzgerald"]) .as_("byExcludedAuthors") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select( notEqualAny(field("author"), Arrays.asList("George Orwell", "F. Scott Fitzgerald")) .as("byExcludedAuthors")) .execute() .get();
สูงสุด
ไวยากรณ์:
maximum(x: ANY...) -> ANY
maximum(x: ARRAY) -> ANY
คำอธิบาย:
แสดงผลค่าสูงสุดที่ไม่ใช่ NULL และไม่ใช่ ABSENT ในชุดค่า x
หากไม่มีค่าที่ไม่ใช่ NULL และไม่ใช่ ABSENT ระบบจะแสดงผล NULL
หากมีค่าเทียบเท่าสูงสุดหลายค่า ระบบจะแสดงค่าใดค่าหนึ่ง การจัดลำดับประเภทค่าจะเป็นไปตามการจัดลำดับที่ระบุไว้
ตัวอย่าง
x |
y |
maximum(x, y) |
|---|---|---|
FALSE |
TRUE |
TRUE |
FALSE |
-10L | -10L |
| 0.0 | -5L | 0.0 |
| "foo" | "bar" | "foo" |
| "foo" | ["foo"] | ["foo"] |
ABSENT |
ABSENT |
NULL |
NULL |
NULL |
NULL |
Node.js
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) );
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("rating").logicalMaximum([1]).as("flooredRating") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("rating").logicalMaximum(1).alias("flooredRating") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("rating").logicalMaximum(1).alias("flooredRating") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").logical_maximum(1).as_("flooredRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(logicalMaximum(field("rating"), 1).as("flooredRating")) .execute() .get();
ขั้นต่ำ
ไวยากรณ์:
minimum(x: ANY...) -> ANY
minimum(x: ARRAY) -> ANY
คำอธิบาย:
แสดงผลค่าต่ำสุดที่ไม่ใช่ NULL และไม่ใช่ ABSENT ในชุดค่า x
หากไม่มีค่าที่ไม่ใช่ NULL และไม่ใช่ ABSENT ระบบจะแสดงผล NULL
หากมีค่าเทียบเท่าขั้นต่ำหลายค่า ระบบจะแสดงค่าใดค่าหนึ่ง การจัดลำดับประเภทค่าจะเป็นไปตามการจัดลำดับที่ระบุไว้
ตัวอย่าง
x |
y |
minimum(x, y) |
|---|---|---|
FALSE |
TRUE |
FALSE |
FALSE |
-10L | FALSE |
| 0.0 | -5L | -5L |
| "foo" | "bar" | "bar" |
| "foo" | ["foo"] | "foo" |
ABSENT |
ABSENT |
NULL |
NULL |
NULL |
NULL |
Node.js
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) );
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("rating").logicalMinimum([5]).as("cappedRating") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("rating").logicalMinimum(5).alias("cappedRating") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("rating").logicalMinimum(5).alias("cappedRating") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("rating").logical_minimum(5).as_("cappedRating")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(logicalMinimum(field("rating"), 5).as("cappedRating")) .execute() .get();
ฟังก์ชันแผนที่
| ชื่อ | คำอธิบาย |
MAP
|
สร้างค่าแมปจากชุดคู่คีย์-ค่า |
MAP_GET
|
แสดงผลค่าในแมปเมื่อระบุคีย์ |
MAP_SET
|
แสดงผลสำเนาของแผนที่ที่มีชุดคีย์ที่อัปเดตแล้ว |
MAP_REMOVE
|
แสดงผลสำเนาของแผนที่โดยนำชุดคีย์ออก |
MAP_MERGE
|
รวมชุดแผนที่เข้าด้วยกัน |
CURRENT_CONTEXT
|
แสดงผลบริบทปัจจุบันเป็นแผนที่ |
MAP_KEYS
|
แสดงผลอาร์เรย์ของคีย์ทั้งหมดในแมป |
MAP_VALUES
|
แสดงผลอาร์เรย์ของค่าทั้งหมดในแผนที่ |
MAP_ENTRIES
|
แสดงผลอาร์เรย์ของคู่คีย์-ค่าของแผนที่ |
MAP
ไวยากรณ์:
map(key: STRING, value: ANY, ...) -> MAP
คำอธิบาย:
สร้างแมปจากชุดคู่คีย์-ค่า
MAP_GET
ไวยากรณ์:
map_get(map: ANY, key: STRING) -> ANY
คำอธิบาย:
แสดงผลค่าในแผนที่เมื่อระบุคีย์ แสดงผลค่า ABSENT หาก key ไม่มีอยู่ในแมป หรือหากอาร์กิวเมนต์ map ไม่ใช่ MAP
Node.js
const result = await db.pipeline() .collection("books") .select( field("awards").mapGet("pulitzer").as("hasPulitzerAward") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("awards").mapGet("pulitzer").as("hasPulitzerAward") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("awards").mapGet("pulitzer").as("hasPulitzerAward") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("awards").mapGet("pulitzer").alias("hasPulitzerAward") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("awards").mapGet("pulitzer").alias("hasPulitzerAward") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("awards").map_get("pulitzer").as_("hasPulitzerAward")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(mapGet(field("awards"), "pulitzer").as("hasPulitzerAward")) .execute() .get();
MAP_SET
ไวยากรณ์:
map_set(map: MAP, key: STRING, value: ANY, ...) -> MAP
คำอธิบาย:
แสดงผลสำเนาของค่า map โดยอัปเดตเนื้อหาด้วยชุดคู่คีย์-ค่า
หากค่าที่ระบุไม่พบ ระบบจะนำคีย์ที่เชื่อมโยงออกจากแผนที่
หากmapอาร์กิวเมนต์ไม่ใช่ MAP จะแสดงผลค่าที่ไม่มี
MAP_REMOVE
ไวยากรณ์:
map_remove(map: MAP, key: STRING...) -> MAP
คำอธิบาย:
แสดงผลสำเนาของค่า map โดยนำชุดคีย์ออก
MAP_MERGE
ไวยากรณ์:
map_merge(maps: MAP...) -> MAP
ผสานเนื้อหาของแผนที่ตั้งแต่ 2 แผนที่ขึ้นไป หากแผนที่หลายรายการมีค่าที่ขัดแย้งกัน ระบบจะใช้ค่าสุดท้าย
CURRENT_CONTEXT
ไวยากรณ์:
current_context() -> MAP
แสดงผลแมปซึ่งประกอบด้วยฟิลด์ที่พร้อมใช้งานทั้งหมดในจุดที่ดำเนินการปัจจุบัน
MAP_KEYS
ไวยากรณ์:
map_keys(map: MAP) -> ARRAY<STRING>
คำอธิบาย:
แสดงผลอาร์เรย์ที่มีคีย์ทั้งหมดของmap value
MAP_VALUES
ไวยากรณ์:
map_values(map: MAP) -> ARRAY<ANY>
คำอธิบาย:
แสดงผลอาร์เรย์ที่มีค่าทั้งหมดของค่า map
MAP_ENTRIES
ไวยากรณ์:
map_entries(map: MAP) -> ARRAY<MAP>
คำอธิบาย:
แสดงผลอาร์เรย์ที่มีคู่คีย์-ค่าทั้งหมดในmapค่า
คู่คีย์-ค่าแต่ละคู่จะอยู่ในรูปแบบของแผนที่ที่มี 2 รายการ ได้แก่ k และ v
ตัวอย่าง
map |
map_entries(map) |
|---|---|
| {} | [] |
| {"foo" : 2L} | [{"k": "foo", "v" : 2L}] |
| {"foo" : "bar", "bar" : "foo"} | [{"k": "foo", "v" : "bar" }, {"k" : "bar", "v": "foo"}] |
ฟังก์ชันสตริง
| ชื่อ | คำอธิบาย |
BYTE_LENGTH
|
แสดงผลจำนวน BYTES ในค่า STRING หรือ BYTES
|
CHAR_LENGTH
|
แสดงผลจำนวนอักขระ Unicode ใน STRING value
|
STARTS_WITH
|
แสดง TRUE หาก STRING ขึ้นต้นด้วยคำนำหน้าที่ระบุ
|
ENDS_WITH
|
แสดง TRUE หาก STRING ลงท้ายด้วยคำต่อท้ายที่ระบุ
|
LIKE
|
แสดงผล TRUE หาก STRING ตรงกับรูปแบบ
|
REGEX_CONTAINS
|
แสดงผล TRUE หากค่าตรงกับนิพจน์ทั่วไปทั้งหมดหรือบางส่วน
|
REGEX_MATCH
|
แสดงผล TRUE หากค่าส่วนใดส่วนหนึ่งตรงกับนิพจน์ทั่วไป
|
STRING_CONCAT
|
ต่อ STRING หลายรายการเป็น STRING
|
STRING_CONTAINS
|
แสดงผล TRUE หากค่ามี STRING
|
STRING_INDEX_OF
|
แสดงผลดัชนีที่อิงตาม 0 ของค่า STRING หรือ BYTES ที่เกิดขึ้นครั้งแรก
|
TO_UPPER
|
แปลงค่า STRING หรือ BYTES เป็นตัวพิมพ์ใหญ่
|
TO_LOWER
|
แปลงค่า STRING หรือ BYTES เป็นตัวพิมพ์เล็ก
|
SUBSTRING
|
รับสตริงย่อยของค่า STRING หรือ BYTES
|
STRING_REVERSE
|
กลับค่า STRING หรือ BYTES
|
STRING_REPEAT
|
ทำซ้ำค่า STRING หรือ BYTES ตามจำนวนครั้งที่ระบุ
|
STRING_REPLACE_ALL
|
แทนที่ค่า STRING หรือ BYTES ทั้งหมด
|
STRING_REPLACE_ONE
|
แทนที่ค่า STRING หรือ BYTES ที่เกิดขึ้นครั้งแรก
|
TRIM
|
ตัดอักขระนำและอักขระต่อท้ายออกจากค่า STRING หรือ BYTES
|
LTRIM
|
ตัดอักขระนำหน้าออกจากค่า STRING หรือ BYTES
|
RTRIM
|
ตัดอักขระต่อท้ายออกจากค่า STRING หรือ BYTES
|
SPLIT
|
แยกค่า STRING หรือ BYTES ออกเป็นอาร์เรย์
|
BYTE_LENGTH
ไวยากรณ์:
byte_length[T <: STRING | BYTES](value: T) -> INT64
คำอธิบาย:
แสดงผลจำนวน BYTES ในค่า STRING หรือ BYTES
ตัวอย่าง
| value | byte_length(value) |
|---|---|
| "abc" | 3 |
| "xyzabc" | 6 |
| b"abc" | 3 |
Node.js
const result = await db.pipeline() .collection("books") .select( field("title").byteLength().as("titleByteLength") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("title").byteLength().as("titleByteLength") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("title").byteLength().as("titleByteLength") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("title").byteLength().alias("titleByteLength") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("title").byteLength().alias("titleByteLength") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("title").byte_length().as_("titleByteLength")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(byteLength(field("title")).as("titleByteLength")) .execute() .get();
CHAR_LENGTH
ไวยากรณ์:
char_length(value: STRING) -> INT64
คำอธิบาย:
แสดงผลจำนวนจุดรหัส Unicode ในค่า STRING
ตัวอย่าง
| value | char_length(value) |
|---|---|
| "abc" | 3 |
| "สวัสดี" | 5 |
| "โลก" | 5 |
Node.js
const result = await db.pipeline() .collection("books") .select( field("title").charLength().as("titleCharLength") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("title").charLength().as("titleCharLength") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("title").charLength().as("titleCharLength") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("title").charLength().alias("titleCharLength") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("title").charLength().alias("titleCharLength") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("title").char_length().as_("titleCharLength")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(charLength(field("title")).as("titleCharLength")) .execute() .get();
STARTS_WITH
ไวยากรณ์:
starts_with(value: STRING, prefix: STRING) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value ขึ้นต้นด้วย prefix
ตัวอย่าง
| value | คำนำหน้า | starts_with(value, prefix) |
|---|---|---|
| "abc" | "ก" | จริง |
| "abc" | "b" | เท็จ |
| "abc" | "" | จริง |
Node.js
const result = await db.pipeline() .collection("books") .select( field("title").startsWith("The") .as("needsSpecialAlphabeticalSort") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("title").startsWith("The") .as("needsSpecialAlphabeticalSort") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("title").startsWith("The") .as("needsSpecialAlphabeticalSort") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("title").startsWith("The") .alias("needsSpecialAlphabeticalSort") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("title").startsWith("The") .alias("needsSpecialAlphabeticalSort") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("title").starts_with("The").as_("needsSpecialAlphabeticalSort") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(startsWith(field("title"), "The").as("needsSpecialAlphabeticalSort")) .execute() .get();
ENDS_WITH
ไวยากรณ์:
ends_with(value: STRING, postfix: STRING) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value ลงท้ายด้วย postfix
ตัวอย่าง
| value | Postfix | ends_with(value, postfix) |
|---|---|---|
| "abc" | "ค" | จริง |
| "abc" | "b" | เท็จ |
| "abc" | "" | จริง |
Node.js
const result = await db.pipeline() .collection("inventory/devices/laptops") .select( field("name").endsWith("16 inch") .as("16InLaptops") ) .execute();
Swift
let result = try await db.pipeline() .collection("inventory/devices/laptops") .select([ Field("name").endsWith("16 inch") .as("16InLaptops") ]) .execute()
Kotlin
val result = db.pipeline() .collection("inventory/devices/laptops") .select( field("name").endsWith("16 inch") .alias("16InLaptops") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("inventory/devices/laptops") .select( field("name").endsWith("16 inch") .alias("16InLaptops") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("inventory/devices/laptops") .select(Field.of("name").ends_with("16 inch").as_("16InLaptops")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("inventory/devices/laptops") .select(endsWith(field("name"), "16 inch").as("16InLaptops")) .execute() .get();
ชอบ
ไวยากรณ์:
like(value: STRING, pattern: STRING) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value ตรงกับ pattern
ตัวอย่าง
| value | ลาย | like(value, pattern) |
|---|---|---|
| "Firestore" | ไฟ% | จริง |
| "Firestore" | "%store" | จริง |
| "Datastore" | "Data_tore" | จริง |
| "100%" | "100\%" | จริง |
Node.js
const result = await db.pipeline() .collection("books") .select( field("genre").like("%Fiction") .as("anyFiction") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("genre").like("%Fiction") .as("anyFiction") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("genre").like("%Fiction") .as("anyFiction") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("genre").like("%Fiction") .alias("anyFiction") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("genre").like("%Fiction") .alias("anyFiction") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").like("%Fiction").as_("anyFiction")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(like(field("genre"), "%Fiction").as("anyFiction")) .execute() .get();
REGEX_CONTAINS
ไวยากรณ์:
regex_contains(value: STRING, pattern: STRING) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากส่วนใดส่วนหนึ่งของ value ตรงกับ pattern หาก pattern ไม่ใช่นิพจน์ทั่วไปที่ถูกต้อง ฟังก์ชันนี้จะแสดงผล error
นิพจน์ทั่วไปเป็นไปตามไวยากรณ์ของไลบรารี re2
ตัวอย่าง
| value | ลาย | regex_contains(value, pattern) |
|---|---|---|
| "Firestore" | ไฟ | จริง |
| "Firestore" | "store$" | จริง |
| "Firestore" | "ข้อมูล" | เท็จ |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("title").regexContains("Firestore (Enterprise|Standard)") .as("isFirestoreRelated") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("title").regexContains("Firestore (Enterprise|Standard)") .as("isFirestoreRelated") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("title").regexContains("Firestore (Enterprise|Standard)") .as("isFirestoreRelated") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("title").regexContains("Firestore (Enterprise|Standard)") .alias("isFirestoreRelated") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("title").regexContains("Firestore (Enterprise|Standard)") .alias("isFirestoreRelated") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select( Field.of("title") .regex_contains("Firestore (Enterprise|Standard)") .as_("isFirestoreRelated") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select( regexContains(field("title"), "Firestore (Enterprise|Standard)") .as("isFirestoreRelated")) .execute() .get();
REGEX_MATCH
ไวยากรณ์:
regex_match(value: STRING, pattern: STRING) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หาก value ตรงกับ pattern ทุกประการ หาก pattern ไม่ใช่นิพจน์ทั่วไปที่ถูกต้อง ฟังก์ชันนี้จะแสดงผล error
นิพจน์ทั่วไปเป็นไปตามไวยากรณ์ของไลบรารี re2
ตัวอย่าง
| value | ลาย | regex_match(value, pattern) |
|---|---|---|
| "Firestore" | "F.*store" | จริง |
| "Firestore" | ไฟ | เท็จ |
| "Firestore" | "^F.*e$" | จริง |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("title").regexMatch("Firestore (Enterprise|Standard)") .as("isFirestoreExactly") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("title").regexMatch("Firestore (Enterprise|Standard)") .as("isFirestoreExactly") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("title").regexMatch("Firestore (Enterprise|Standard)") .as("isFirestoreExactly") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("title").regexMatch("Firestore (Enterprise|Standard)") .alias("isFirestoreExactly") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("title").regexMatch("Firestore (Enterprise|Standard)") .alias("isFirestoreExactly") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select( Field.of("title") .regex_match("Firestore (Enterprise|Standard)") .as_("isFirestoreExactly") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select( regexMatch(field("title"), "Firestore (Enterprise|Standard)") .as("isFirestoreExactly")) .execute() .get();
STRING_CONCAT
ไวยากรณ์:
string_concat(values: STRING...) -> STRING
คำอธิบาย:
เชื่อมต่อSTRINGค่าอย่างน้อย 2 ค่าเป็นผลลัพธ์เดียว
ตัวอย่าง
| อาร์กิวเมนต์ | string_concat(values...) |
|---|---|
() |
ข้อผิดพลาด |
("a") |
"ก" |
("abc", "def") |
"abcdef" |
("a", "", "c") |
"ac" |
Node.js
const result = await db.pipeline() .collection("books") .select( field("title").stringConcat(" by ", field("author")) .as("fullyQualifiedTitle") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("title").stringConcat(" by ", field("author")) .as("fullyQualifiedTitle") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("title").concat([" by ", Field("author")]) .as("fullyQualifiedTitle") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("title").concat(" by ", field("author")) .alias("fullyQualifiedTitle") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("title").concat(" by ", field("author")) .alias("fullyQualifiedTitle") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("title") .concat(" by ", Field.of("author")) .as_("fullyQualifiedTitle") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(stringConcat(field("title"), " by ", field("author")).as("fullyQualifiedTitle")) .execute() .get();
STRING_CONTAINS
ไวยากรณ์:
string_contains(value: STRING, substring: STRING) -> BOOLEAN
คำอธิบาย:
ตรวจสอบว่า value มีสตริงตามตัวอักษร substring หรือไม่
ตัวอย่าง
| value | สตริงย่อย | string_contains(value, substring) |
|---|---|---|
| "abc" | "b" | จริง |
| "abc" | "d" | เท็จ |
| "abc" | "" | จริง |
| "a.c" | "." | จริง |
| "☃☃☃" | "☃" | จริง |
Node.js
const result = await db.pipeline() .collection("articles") .select( field("body").stringContains("Firestore") .as("isFirestoreRelated") ) .execute();
Web
const result = await execute(db.pipeline() .collection("articles") .select( field("body").stringContains("Firestore") .as("isFirestoreRelated") ) );
Swift
let result = try await db.pipeline() .collection("articles") .select([ Field("body").stringContains("Firestore") .as("isFirestoreRelated") ]) .execute()
Kotlin
val result = db.pipeline() .collection("articles") .select( field("body").stringContains("Firestore") .alias("isFirestoreRelated") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("articles") .select( field("body").stringContains("Firestore") .alias("isFirestoreRelated") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("articles") .select(Field.of("body").string_contains("Firestore").as_("isFirestoreRelated")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("articles") .select(stringContains(field("body"), "Firestore").as("isFirestoreRelated")) .execute() .get();
STRING_INDEX_OF
ไวยากรณ์:
string_index_of[T <: STRING | BYTES](value: T, search: T) -> INT64
คำอธิบาย:
แสดงดัชนีแบบ 0 ของการเกิดครั้งแรกของ search ใน value
- แสดงผล
-1หากไม่พบsearch - หาก
valueเป็นค่าSTRINGระบบจะวัดผลลัพธ์เป็นจุดรหัส Unicode หากเป็นค่าBYTESระบบจะวัดค่าเป็นไบต์ - หาก
searchเป็นค่าSTRINGหรือBYTESที่ว่างเปล่า ผลลัพธ์จะเป็น0
ตัวอย่าง
| value | ค้นหา | string_index_of(value, search) |
|---|---|---|
| "hello world" | "o" | 4 |
| "hello world" | "l" | 2 |
| "hello world" | "z" | -1 |
| "กล้วย" | "na" | 2 |
| "abc" | "" | 0 |
| b"abc" | b"b" | 1 |
| "é" | "é" | 0 |
| b"é" | b"é" | 0 |
TO_UPPER
ไวยากรณ์:
to_upper[T <: STRING | BYTES](value: T) -> T
คำอธิบาย:
แปลงค่า STRING หรือ BYTES เป็นตัวพิมพ์ใหญ่
หากไบต์หรืออักขระไม่สอดคล้องกับอักขระที่เป็นตัวอักษรพิมพ์เล็ก UTF-8 ระบบจะส่งผ่านโดยไม่มีการเปลี่ยนแปลง
ตัวอย่าง
| value | to_upper(value) |
|---|---|
| "abc" | "ABC" |
| "AbC" | "ABC" |
| b"abc" | b"ABC" |
| b"a1c" | b"A1C" |
Node.js
const result = await db.pipeline() .collection("authors") .select( field("name").toUpper() .as("uppercaseName") ) .execute();
Web
const result = await execute(db.pipeline() .collection("authors") .select( field("name").toUpper() .as("uppercaseName") ) );
Swift
let result = try await db.pipeline() .collection("authors") .select([ Field("name").toUpper() .as("uppercaseName") ]) .execute()
Kotlin
val result = db.pipeline() .collection("authors") .select( field("name").toUpper() .alias("uppercaseName") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("authors") .select( field("name").toUpper() .alias("uppercaseName") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("authors") .select(Field.of("name").to_upper().as_("uppercaseName")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("authors") .select(toUpper(field("name")).as("uppercaseName")) .execute() .get();
TO_LOWER
ไวยากรณ์:
to_lower[T <: STRING | BYTES](value: T) -> T
คำอธิบาย:
แปลงค่า STRING หรือ BYTES เป็นตัวพิมพ์เล็ก
หากไบต์หรืออักขระไม่สอดคล้องกับอักขระตัวอักษรตัวพิมพ์ใหญ่ UTF-8 ระบบจะส่งผ่านโดยไม่มีการเปลี่ยนแปลง
ตัวอย่าง
| value | to_lower(value) |
|---|---|
| "ABC" | "abc" |
| "AbC" | "abc" |
| "A1C" | "a1c" |
| b"ABC" | b"abc" |
Node.js
const result = await db.pipeline() .collection("authors") .select( field("genre").toLower().equal("fantasy") .as("isFantasy") ) .execute();
Web
const result = await execute(db.pipeline() .collection("authors") .select( field("genre").toLower().equal("fantasy") .as("isFantasy") ) );
Swift
let result = try await db.pipeline() .collection("authors") .select([ Field("genre").toLower().equal("fantasy") .as("isFantasy") ]) .execute()
Kotlin
val result = db.pipeline() .collection("authors") .select( field("genre").toLower().equal("fantasy") .alias("isFantasy") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("authors") .select( field("genre").toLower().equal("fantasy") .alias("isFantasy") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("authors") .select(Field.of("genre").to_lower().equal("fantasy").as_("isFantasy")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("authors") .select(equal(toLower(field("genre")), "fantasy").as("isFantasy")) .execute() .get();
SUBSTRING
ไวยากรณ์:
substring[T <: STRING | BYTES](input: T, position: INT64) -> T
substring[T <: STRING | BYTES](input: T, position: INT64, length: INT64) -> T
คำอธิบาย:
แสดงผลสตริงย่อยของ input โดยเริ่มที่ position (ดัชนีที่อิงตาม 0) และรวมรายการสูงสุด length รายการ หากไม่ได้ระบุ length ระบบจะแสดงสตริงย่อย
จาก position ไปจนถึงท้าย input
หาก
inputเป็นค่าSTRINGระบบจะวัดpositionและlengthในจุดรหัส Unicode หากเป็นค่าBYTESระบบจะวัดค่าเป็นไบต์หาก
positionมากกว่าความยาวของinputระบบจะแสดงผลสตริงย่อยที่ว่างเปล่า หากpositionบวกlengthยาวกว่าความยาวของinputระบบจะตัดสตริงย่อยให้สิ้นสุดที่inputหาก
positionเป็นค่าลบ ระบบจะใช้ตำแหน่งจากท้ายอินพุต หากค่าลบของpositionมากกว่าขนาดของอินพุต ระบบจะตั้งค่าตำแหน่งเป็น 0lengthต้องไม่เป็นค่าลบ
ตัวอย่าง
เมื่อไม่ได้ระบุ length
| อินพุต | position | substring(input, position) |
|---|---|---|
| "abc" | 0 | "abc" |
| "abc" | 1 | "bc" |
| "abc" | 3 | "" |
| "abc" | -1 | "ค" |
| b"abc" | 1 | b"bc" |
เมื่อมีการระบุlength
| อินพุต | position | ความยาว | substring(input, position, length) |
|---|---|---|---|
| "abc" | 0 | 1 | "ก" |
| "abc" | 1 | 2 | "bc" |
| "abc" | -1 | 1 | "ค" |
| b"abc" | 0 | 1 | b"a" |
Node.js
const result = await db.pipeline() .collection("books") .where(field("title").startsWith("The ")) .select( field("title").substring(4) .as("titleWithoutLeadingThe") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .where(field("title").startsWith("The ")) .select( field("title").substring(4) .as("titleWithoutLeadingThe") ) );
Swift
let result = try await db.pipeline() .collection("books") .where(Field("title").startsWith("The ")) .select([ Field("title").substring(position: 4) .as("titleWithoutLeadingThe") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .where(field("title").startsWith("The ")) .select( field("title") .substring(constant(4), field("title").charLength().subtract(4)) .alias("titleWithoutLeadingThe") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .where(field("title").startsWith("The ")) .select( field("title").substring( constant(4), field("title").charLength().subtract(4)) .alias("titleWithoutLeadingThe") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .where(Field.of("title").starts_with("The ")) .select(Field.of("title").substring(4).as_("titleWithoutLeadingThe")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .where(startsWith(field("title"), "The ")) .select( substring(field("title"), constant(4), field("title").charLength()) .as("titleWithoutLeadingThe")) .execute() .get();
STRING_REVERSE
ไวยากรณ์:
string_reverse[T <: STRING | BYTES](input: T) -> T
คำอธิบาย:
แสดงผลอินพุตที่ระบุในลำดับย้อนกลับ
ระบบจะกำหนดขอบเขตอักขระด้วยโค้ดพอยต์ Unicode เมื่ออินพุตเป็น STRING และไบต์เมื่ออินพุตเป็นค่า BYTES
ตัวอย่าง
| อินพุต | string_reverse(input) |
|---|---|
| "abc" | "cba" |
| "a🌹b" | "b🌹a" |
| "สวัสดี" | "olleh" |
| b"abc" | b"cba" |
Node.js
const result = await db.pipeline() .collection("books") .select( field("name").reverse().as("reversedName") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("name").reverse().as("reversedName") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("name").reverse().as("reversedName") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("name").reverse().alias("reversedName") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("name").reverse().alias("reversedName") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("name").string_reverse().as_("reversedName")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(reverse(field("name")).as("reversedName")) .execute() .get();
STRING_REPEAT
ไวยากรณ์:
string_repeat[T <: STRING | BYTES](input: T, repetitions: INT64) -> T
คำอธิบาย:
แสดงผล input ซ้ำ repetitions ครั้ง
repetitionsต้องเป็นจำนวนเต็มที่ไม่เป็นลบ- หาก
repetitionsเป็น0จะแสดงผลค่าว่างที่มีประเภทเดียวกับinput - หากผลลัพธ์มีขนาดเกินขนาดสูงสุดที่อนุญาต (1 MB) ระบบจะแสดงข้อผิดพลาด
ตัวอย่าง
| อินพุต | การทำซ้ำ | string_repeat(input, repetitions) |
|---|---|---|
| "foo" | 3 | "foofoofoo" |
| "foo" | 0 | "" |
| "a " | 3 | "a a a " |
| b"ab" | 2 | b"abab" |
| "é🦆" | 2 | "é🦆é🦆" |
STRING_REPLACE_ALL
ไวยากรณ์:
string_replace_all[T <: STRING | BYTES](input: T, find: T, replacement: T) -> T
คำอธิบาย:
แทนที่อินสแตนซ์ทั้งหมดของ find ใน input ด้วย replacement
- การจับคู่จะคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
- หาก
findว่างเปล่า ระบบจะไม่ทำการแทนที่
ตัวอย่าง
| อินพุต | ค้นหา | การเปลี่ยนทดแทน | string_replace_all(input, find, replacement) |
|---|---|---|---|
| "foobarfoo" | "foo" | "baz" | "bazbarbaz" |
| "ababab" | "aba" | "ค" | "cbab" |
| "foobar" | "o" | "" | "fbar" |
| "é🦆🌎🦆" | "🦆" | "ก" | "éa🌎a" |
| b"abc" | b"b" | b"d" | b"adc" |
STRING_REPLACE_ONE
ไวยากรณ์:
string_replace_one[T <: STRING | BYTES](input: T, find: T, replacement: T) -> T
คำอธิบาย:
แทนที่อินสแตนซ์แรกของ find ใน input ด้วย replacement
- การจับคู่จะคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
- หาก
findว่างเปล่า ระบบจะไม่ทำการแทนที่
ตัวอย่าง
| อินพุต | ค้นหา | การเปลี่ยนทดแทน | string_replace_one(input, find, replacement) |
|---|---|---|---|
| "foobarfoo" | "foo" | "baz" | "bazbarfoo" |
| "é" | "é" | "ก" | "ก" |
| b"foobar" | b"o" | b"z" | b"fzoobar" |
TRIM
ไวยากรณ์:
trim[T <: STRING | BYTES](input: T, values_to_trim: T) -> T
trim[T <: STRING | BYTES](input: T) -> T
คำอธิบาย:
ตัดชุด BYTES หรือ CHARS ที่ระบุออกจากจุดเริ่มต้นและจุดสิ้นสุดของ input ที่ระบุ
- หากไม่ได้ระบุ
values_to_trimระบบจะตัดอักขระช่องว่าง
ตัวอย่าง
เมื่อไม่ได้ระบุ values_to_trim
| อินพุต | trim(input) |
|---|---|
| " foo " | "foo" |
| b" foo " | b"foo" |
| "foo" | "foo" |
| "" | "" |
| " " | "" |
| "\t foo \n" | "foo" |
| b"\t foo \n" | b"foo" |
| "\r\f\v foo \r\f\v" | "foo" |
| b"\r\f\v foo \r\f\v" | b"foo" |
เมื่อมีการระบุvalues_to_trim
| อินพุต | values_to_trim | trim(input, values_to_trim) |
|---|---|---|
| "abcbfooaacb" | "abc" | "foo" |
| "abcdaabadbac" | "abc" | "daabad" |
| b"C1C2C3" | b"C1" | b"C2C3" |
| b"C1C2" | "foo" | ข้อผิดพลาด |
| "foo" | b"C1" | ข้อผิดพลาด |
Web
const result = await execute(db.pipeline() .collection("books") .select( field("name").trim().as("whitespaceTrimmedName") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("name").trim(" \n\t").as("whitespaceTrimmedName") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("name").trim().alias("whitespaceTrimmedName") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("name").trim().alias("whitespaceTrimmedName") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("name").trim().as_("whitespaceTrimmedName")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(trim(field("name")).as("whitespaceTrimmedName")) .execute() .get();
LTRIM
ไวยากรณ์:
ltrim[T <: STRING | BYTES](value: T, to_trim: T) -> T
ltrim[T <: STRING | BYTES](value: T) -> T
คำอธิบาย:
ตัดชุด BYTES หรือ CHARS ที่ระบุออกจากจุดเริ่มต้นของ value ที่ระบุ
- หากไม่ได้ระบุ
to_trimระบบจะตัดอักขระช่องว่างนำหน้า
ตัวอย่าง
เมื่อไม่ได้ระบุ to_trim
| value | ltrim(value) |
|---|---|
| " foo " | "foo " |
| "foo" | "foo" |
เมื่อมีการระบุto_trim
| value | to_trim | ltrim(value, to_trim) |
|---|---|---|
| "aaabc" | "ก" | "bc" |
| "abacaba" | "ba" | "caba" |
| "é" | "é" | "" |
RTRIM
ไวยากรณ์:
rtrim[T <: STRING | BYTES](value: T, to_trim: T) -> T
rtrim[T <: STRING | BYTES](value: T) -> T
คำอธิบาย:
ตัดชุด BYTES หรือ CHARS ที่ระบุออกจากตอนท้ายของ value ที่ระบุ
- หากไม่ได้ระบุ
to_trimระบบจะตัดอักขระช่องว่างต่อท้าย
ตัวอย่าง
เมื่อไม่ได้ระบุ to_trim
| value | rtrim(value) |
|---|---|
| " foo " | " foo" |
| "foo" | "foo" |
เมื่อมีการระบุto_trim
| value | to_trim | rtrim(value, to_trim) |
|---|---|---|
| "abccc" | "ค" | "ab" |
| "abacaba" | "ba" | "abac" |
| "é" | "é" | "" |
SPLIT
ไวยากรณ์:
split(input: STRING) -> ARRAY<STRING>
split[T <: STRING | BYTES](input: T, delimiter: T) -> ARRAY<T>
คำอธิบาย:
แยกค่า STRING หรือ BYTES โดยใช้ตัวคั่น
สำหรับ
STRINGตัวคั่นเริ่มต้นคือคอมมา,ระบบจะถือว่าตัวคั่นเป็นสตริงเดียวสำหรับ
BYTESคุณต้องระบุตัวคั่นการแยกตามตัวคั่นที่ว่างเปล่าจะสร้างอาร์เรย์ของโค้ดพอยต์ Unicode สำหรับค่า
STRINGและอาร์เรย์ของBYTESสำหรับค่าBYTESการแยก
STRINGที่ว่างเปล่าจะแสดงผลARRAYที่มีSTRINGว่างเปล่ารายการเดียว
ตัวอย่าง
เมื่อไม่ได้ระบุ delimiter
| อินพุต | split(input) |
|---|---|
| "foo,bar,foo" | ["foo", "bar", "foo"] |
| "foo" | ["foo"] |
| ",foo," | ["", "foo", ""] |
| "" | [""] |
| b"C120C2C4" | ข้อผิดพลาด |
เมื่อมีการระบุdelimiter
| อินพุต | ตัวคั่น | split(input, delimiter) |
|---|---|---|
| "foo bar foo" | " " | ["foo", "bar", "foo"] |
| "foo bar foo" | "z" | ["foo bar foo"] |
| "abc" | "" | ["a", "b", "c"] |
| b"C1,C2,C4" | b"," | [b"C1", b"C2", b"C4"] |
| b"ABC" | b"" | [b"A", b"B", b"C"] |
| "foo" | b"C1" | ข้อผิดพลาด |
ฟังก์ชันการประทับเวลา
| ชื่อ | คำอธิบาย |
CURRENT_TIMESTAMP
|
สร้าง TIMESTAMP ที่สอดคล้องกับเวลาที่ส่งคำขอ
|
TIMESTAMP_TRUNC
|
ตัดทอน TIMESTAMP ให้เป็นระดับรายละเอียดที่ระบุ
|
UNIX_MICROS_TO_TIMESTAMP
|
แปลงจำนวนไมโครวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC เป็น TIMESTAMP
|
UNIX_MILLIS_TO_TIMESTAMP
|
แปลงจำนวนมิลลิวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC เป็น TIMESTAMP
|
UNIX_SECONDS_TO_TIMESTAMP
|
แปลงจำนวนวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC เป็น TIMESTAMP
|
TIMESTAMP_ADD
|
เพิ่มช่วงเวลาลงใน TIMESTAMP
|
TIMESTAMP_SUB
|
ลบช่วงเวลาออกจาก TIMESTAMP
|
TIMESTAMP_TO_UNIX_MICROS
|
แปลง TIMESTAMP เป็นจำนวนไมโครวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC
|
TIMESTAMP_TO_UNIX_MILLIS
|
แปลง TIMESTAMP เป็นจำนวนมิลลิวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC
|
TIMESTAMP_TO_UNIX_SECONDS
|
แปลง TIMESTAMP เป็นจำนวนวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC
|
TIMESTAMP_DIFF
|
แสดงผลจำนวนเต็มของช่วงเวลา unit ที่ระบุระหว่าง TIMESTAMP 2 รายการ
|
TIMESTAMP_EXTRACT
|
ดึงข้อมูล part ที่เฉพาะเจาะจง (เช่น ปี เดือน วัน) จาก TIMESTAMP
|
CURRENT_TIMESTAMP
ไวยากรณ์:
current_timestamp() -> TIMESTAMP
คำอธิบาย:
รับการประทับเวลาที่จุดเริ่มต้นของเวลาคำขอ input (ตีความเป็นจำนวนไมโครวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC)
ค่านี้จะคงที่ภายในคำค้นหา และจะแปลงเป็นค่าเดียวกันเสมอหากมีการเรียกใช้หลายครั้ง
TIMESTAMP_TRUNC
ไวยากรณ์:
timestamp_trunc(timestamp: TIMESTAMP, granularity: STRING[, timezone: STRING]) -> TIMESTAMP
คำอธิบาย:
ตัดทอนการประทับเวลาลงไปที่ระดับรายละเอียดที่กำหนด
อาร์กิวเมนต์ granularity ต้องเป็นสตริงและเป็นอย่างใดอย่างหนึ่งต่อไปนี้
microsecondmillisecondsecondminutehourdayweekweek([weekday])monthquarteryearisoyear
หากระบุอาร์กิวเมนต์ timezone การตัดทอนจะอิงตามขอบเขตปฏิทินของเขตเวลาที่ระบุ (เช่น การตัดทอนวันจะตัดทอนเป็นเที่ยงคืนในเขตเวลาที่ระบุ) การตัดทอนจะเป็นไปตามเวลาออมแสง
หากไม่ได้ระบุ timezone ระบบจะตัดข้อความตามขอบเขตปฏิทิน UTC
อาร์กิวเมนต์ timezone ควรเป็นสตริงที่แสดงเขตเวลาจากฐานข้อมูล tz เช่น America/New_York นอกจากนี้ คุณยังใช้ออฟเซ็ตเวลาที่กำหนดเองได้โดยระบุออฟเซ็ตจาก GMT
ตัวอย่าง
timestamp |
granularity |
timezone |
timestamp_trunc(timestamp, granularity, timezone) |
|---|---|---|---|
| 2000-01-01 10:20:30:123456 UTC | "วินาที" | ไม่ได้ระบุข้อมูล | 01-01-2001 10:20:30 UTC |
| 1997-05-31 04:30:30 UTC | "วัน" | ไม่ได้ระบุข้อมูล | 1997-05-31 00:00:00 UTC |
| 1997-05-31 04:30:30 UTC | "วัน" | "America/Los_Angeles" | 1997-05-30 07:00:00 UTC |
| 16-03-2001 04:00:00 UTC | "week(friday) | ไม่ได้ระบุข้อมูล | 16-03-2001 00:00:00 UTC |
| 23-03-2001 04:00:00 UTC | "week(friday) | "America/Los_Angeles" | 23-03-2001 17:00:00 UTC |
| 24-01-2026 20:00:00 UTC | "เดือน" | "GMT+06:32:43" | 2026-01-01T06:32:43 UTC |
UNIX_MICROS_TO_TIMESTAMP
ไวยากรณ์:
unix_micros_to_timestamp(input: INT64) -> TIMESTAMP
คำอธิบาย:
แปลง input (ตีความเป็นจํานวนไมโครวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC) เป็น TIMESTAMP แสดง error หากแปลง input เป็น TIMESTAMP ที่ถูกต้องไม่ได้
ตัวอย่าง
input |
unix_micros_to_timestamp(input) |
|---|---|
| 0L | 1970-01-01 00:00:00 UTC |
| 400123456L | 1970-01-01 00:06:40.123456 UTC |
| -1000000L | 1969-12-31 23:59:59 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("createdAtMicros").unixMicrosToTimestamp().alias("createdAtString") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("createdAtMicros").unixMicrosToTimestamp().alias("createdAtString") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select( Field.of("createdAtMicros") .unix_micros_to_timestamp() .as_("createdAtString") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(unixMicrosToTimestamp(field("createdAtMicros")).as("createdAtString")) .execute() .get();
UNIX_MILLIS_TO_TIMESTAMP
ไวยากรณ์:
unix_millis_to_timestamp(input: INT64) -> TIMESTAMP
คำอธิบาย:
แปลง input (ตีความเป็นจำนวนมิลลิวินาทีนับตั้งแต่ 1970-01-01 00:00:00 UTC) เป็น TIMESTAMP แสดง error หากแปลง input เป็น TIMESTAMP ที่ถูกต้องไม่ได้
ตัวอย่าง
input |
unix_millis_to_timestamp(input) |
|---|---|
| 0L | 1970-01-01 00:00:00 UTC |
| 4000123L | 1970-01-01 01:06:40.123 UTC |
| -1000000L | 1969-12-31 23:43:20 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("createdAtMillis").unixMillisToTimestamp().alias("createdAtString") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("createdAtMillis").unixMillisToTimestamp().alias("createdAtString") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select( Field.of("createdAtMillis") .unix_millis_to_timestamp() .as_("createdAtString") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(unixMillisToTimestamp(field("createdAtMillis")).as("createdAtString")) .execute() .get();
UNIX_SECONDS_TO_TIMESTAMP
ไวยากรณ์:
unix_seconds_to_timestamp(input: INT64) -> TIMESTAMP
คำอธิบาย:
แปลง input (ตีความเป็นจำนวนวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC) เป็น TIMESTAMP แสดง error หากแปลง input เป็น TIMESTAMP ที่ถูกต้องไม่ได้
ตัวอย่าง
input |
unix_seconds_to_timestamp(input) |
|---|---|
| 0L | 1970-01-01 00:00:00 UTC |
| 60L | 1970-01-01 00:01:00 UTC |
| -300L | 1969-12-31 23:55:00 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("createdAtSeconds").unixSecondsToTimestamp().alias("createdAtString") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("createdAtSeconds").unixSecondsToTimestamp().alias("createdAtString") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select( Field.of("createdAtSeconds") .unix_seconds_to_timestamp() .as_("createdAtString") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(unixSecondsToTimestamp(field("createdAtSeconds")).as("createdAtString")) .execute() .get();
TIMESTAMP_ADD
ไวยากรณ์:
timestamp_add(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP
คำอธิบาย:
เพิ่มamountของunitจากtimestamp อาร์กิวเมนต์ amount อาจเป็นค่าลบ ในกรณีนี้จะเทียบเท่ากับ TIMESTAMP_SUB
อาร์กิวเมนต์ unit ต้องเป็นสตริงและเป็นอย่างใดอย่างหนึ่งต่อไปนี้
microsecondmillisecondsecondminutehourday
แสดงข้อผิดพลาดหากการประทับเวลาที่ได้ไม่อยู่ในช่วง TIMESTAMP
ตัวอย่าง
timestamp |
unit |
amount |
timestamp_add(timestamp, unit, amount) |
|---|---|---|---|
| 2025-02-20 00:00:00 UTC | "นาที" | 2L | 2025-02-20 00:02:00 UTC |
| 2025-02-20 00:00:00 UTC | "ชั่วโมง" | -4L | 2025-02-19 20:00:00 UTC |
| 2025-02-20 00:00:00 UTC | "วัน" | 5L | 2025-02-25 00:00:00 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("createdAt").timestampAdd("day", 3653).as("expiresAt") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("createdAt").timestampAdd("day", 3653).as("expiresAt") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("createdAt").timestampAdd(3653, .day).as("expiresAt") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("createdAt") .timestampAdd("day", 3653) .alias("expiresAt") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("createdAt").timestampAdd("day", 3653).alias("expiresAt") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select(Field.of("createdAt").timestamp_add("day", 3653).as_("expiresAt")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampAdd(field("createdAt"), "day", 3653).as("expiresAt")) .execute() .get();
TIMESTAMP_SUB
ไวยากรณ์:
timestamp_sub(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP
คำอธิบาย:
ลบ amount ของ unit ออกจาก timestamp อาร์กิวเมนต์ amount อาจเป็นค่าลบ ในกรณีนี้จะเทียบเท่ากับ TIMESTAMP_ADD
อาร์กิวเมนต์ unit ต้องเป็นสตริงและเป็นอย่างใดอย่างหนึ่งต่อไปนี้
microsecondmillisecondsecondminutehourday
แสดงข้อผิดพลาดหากการประทับเวลาที่ได้ไม่อยู่ในช่วง TIMESTAMP
ตัวอย่าง
timestamp |
unit |
amount |
timestamp_sub(timestamp, unit, amount) |
|---|---|---|---|
| 2026-07-04 00:00:00 UTC | "นาที" | 40L | 2026-07-03 23:20:00 UTC |
| 2026-07-04 00:00:00 UTC | "ชั่วโมง" | -24L | 05-07-2026 00:00:00 UTC |
| 2026-07-04 00:00:00 UTC | "วัน" | 3L | 2026-07-01 00:00:00 UTC |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("expiresAt").timestampSubtract(14, .day).as("sendWarningTimestamp") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("expiresAt") .timestampSubtract("day", 14) .alias("sendWarningTimestamp") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("expiresAt").timestampSubtract("day", 14).alias("sendWarningTimestamp") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select( Field.of("expiresAt") .timestamp_subtract("day", 14) .as_("sendWarningTimestamp") ) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampSubtract(field("expiresAt"), "day", 14).as("sendWarningTimestamp")) .execute() .get();
TIMESTAMP_TO_UNIX_MICROS
ไวยากรณ์:
timestamp_to_unix_micros(input: TIMESTAMP) -> INT64
คำอธิบาย:
แปลง input เป็นจำนวนไมโครวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC ตัดระดับความแม่นยำที่สูงกว่าโดยการปัดเศษลงไปที่จุดเริ่มต้นของไมโครวินาที
ตัวอย่าง
input |
timestamp_to_unix_micros(input) |
|---|---|
| 1970-01-01 00:00:00 UTC | 0L |
| 1970-01-01 00:06:40.123456 UTC | 400123456L |
| 1969-12-31 23:59:59 UTC | -1000000L |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMicros().as("unixMicros") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMicros().as("unixMicros") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("dateString").timestampToUnixMicros().as("unixMicros") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMicros().alias("unixMicros") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMicros().alias("unixMicros") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select(Field.of("dateString").timestamp_to_unix_micros().as_("unixMicros")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampToUnixMicros(field("dateString")).as("unixMicros")) .execute() .get();
TIMESTAMP_TO_UNIX_MILLIS
ไวยากรณ์:
timestamp_to_unix_millis(input: TIMESTAMP) -> INT64
คำอธิบาย:
แปลง input เป็นจำนวนมิลลิวินาทีนับตั้งแต่ 1970-01-01 00:00:00 UTC ตัดระดับความแม่นยำที่สูงขึ้นโดยการปัดเศษลงไปที่จุดเริ่มต้นของมิลลิวินาที
ตัวอย่าง
input |
timestamp_to_unix_millis(input) |
|---|---|
| 1970-01-01 00:00:00 UTC | 0L |
| 1970-01-01 01:06:40.123 UTC | 4000123L |
| 1969-12-31 23:43:20 | -1000000L |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMillis().as("unixMillis") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMillis().as("unixMillis") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("dateString").timestampToUnixMillis().as("unixMillis") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMillis().alias("unixMillis") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMillis().alias("unixMillis") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select(Field.of("dateString").timestamp_to_unix_millis().as_("unixMillis")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampToUnixMillis(field("dateString")).as("unixMillis")) .execute() .get();
TIMESTAMP_TO_UNIX_SECONDS
ไวยากรณ์:
timestamp_to_unix_seconds(input: TIMESTAMP) -> INT64
คำอธิบาย:
แปลง input เป็นจำนวนวินาทีตั้งแต่ 1970-01-01 00:00:00 UTC ตัดความแม่นยำระดับสูงกว่าโดยการปัดเศษลงไปที่จุดเริ่มต้นของวินาที
ตัวอย่าง
input |
timestamp_to_unix_seconds(input) |
|---|---|
| 1970-01-01 00:00:00 UTC | 0L |
| 1970-01-01 00:01:00 UTC | 60L |
| 1969-12-31 23:55:00 UTC | -300L |
Node.js
const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixSeconds().as("unixSeconds") ) .execute();
Web
const result = await execute(db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixSeconds().as("unixSeconds") ) );
Swift
let result = try await db.pipeline() .collection("documents") .select([ Field("dateString").timestampToUnixSeconds().as("unixSeconds") ]) .execute()
Kotlin
val result = db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixSeconds().alias("unixSeconds") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixSeconds().alias("unixSeconds") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("documents") .select(Field.of("dateString").timestamp_to_unix_seconds().as_("unixSeconds")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("documents") .select(timestampToUnixSeconds(field("dateString")).as("unixSeconds")) .execute() .get();
TIMESTAMP_DIFF
ไวยากรณ์:
timestamp_diff(end: TIMESTAMP, start: TIMESTAMP, unit: STRING) -> INT64
คำอธิบาย:
แสดงผลจำนวนเต็มของช่วงเวลา unit ที่ระบุระหว่าง TIMESTAMP 2 รายการ
- แสดงผลค่าลบหาก
endอยู่ก่อนstart - ตัดหน่วยที่เป็นเศษส่วน เช่น
timestamp_diff("2021-01-01 00:00:01", "2021-01-01 00:00:00", "minute")จะแสดงผลเป็น0
อาร์กิวเมนต์ unit ต้องเป็นสตริงและเป็นอย่างใดอย่างหนึ่งต่อไปนี้
microsecondmillisecondsecondminutehourday
ตัวอย่าง
end |
start |
unit |
timestamp_diff(end, start, unit) |
|---|---|---|---|
| 2026-07-04 00:01:00 UTC | 2026-07-04 00:00:00 UTC | "วินาที" | 60L |
| 2026-07-04 00:00:00 UTC | 05-07-2026 00:00:00 UTC | "วัน" | -1L |
| 2026-07-04 00:00:59 UTC | 2026-07-04 00:00:00 UTC | "นาที" | 0L |
TIMESTAMP_EXTRACT
ไวยากรณ์:
timestamp_extract(timestamp: TIMESTAMP, part: STRING[, timezone: STRING]) -> INT64
คำอธิบาย:
ดึงข้อมูล part ที่เฉพาะเจาะจง (เช่น ปี เดือน วัน) จาก timestamp
อาร์กิวเมนต์ part ต้องเป็นสตริงและเป็นอย่างใดอย่างหนึ่งต่อไปนี้
microsecondmillisecondsecondminutehourdaydayofweek: แสดงค่าระหว่าง 1 (วันอาทิตย์) ถึง 7 (วันเสาร์)dayofyearweek: แสดงหมายเลขสัปดาห์ของปี โดยเริ่มที่ 1 สำหรับวันอาทิตย์แรกของปีweek([weekday]): แสดงผลหมายเลขสัปดาห์ของปี โดยเริ่มตั้งแต่วันที่weekdayที่ระบุmonthquarteryearisoweek: แสดงผลหมายเลขสัปดาห์ตามมาตรฐาน ISO 8601isoyear: แสดงผลปีตามการกำหนดหมายเลขสัปดาห์ ISO 8601
หากระบุอาร์กิวเมนต์ timezone การดึงข้อมูลจะอิงตามปฏิทินของเขตเวลาที่ระบุ
การแยกข้อมูลจะคำนึงถึงเวลาออมแสง
หากไม่ได้ระบุ timezone ระบบจะดึงข้อมูลโดยอิงตาม UTC
อาร์กิวเมนต์ timezone ควรเป็นสตริงที่แสดงเขตเวลาจาก
ฐานข้อมูลเขตเวลา เช่น America/New_York นอกจากนี้ คุณยังใช้ออฟเซ็ตเวลาที่กำหนดเองได้โดยระบุออฟเซ็ตจาก GMT
ตัวอย่าง
timestamp |
part |
timezone |
timestamp_extract(timestamp, part, timezone) |
|---|---|---|---|
| 2025-02-20 10:20:30 UTC | "year" | ไม่ได้ระบุข้อมูล | 2025 |
| 2025-02-20 10:20:30 UTC | "day" | ไม่ได้ระบุข้อมูล | 20 |
| 2025-12-31 23:59:59 UTC | "year" | "Asia/Tokyo" | 2026 |
ฟังก์ชันประเภท
| ชื่อ | คำอธิบาย |
TYPE
|
แสดงผลประเภทของค่าเป็น STRING
|
IS_TYPE
|
แสดงผล true หากค่าตรงกับประเภทที่ระบุ
|
ประเภท
ไวยากรณ์:
type(input: ANY) -> STRING
คำอธิบาย:
แสดงผลการแสดงสตริงของประเภท input
หากได้รับค่าที่ไม่มีอยู่ จะแสดงผล NULL
ตัวอย่าง
input |
type(input) |
|---|---|
| NULL | "null" |
| จริง | "บูลีน" |
| 1 | "int32" |
| -3L | "int64" |
| 3.14 | "float64" |
| 2024-01-01T00:00:00Z UTC | "timestamp" |
| "foo" | "string" |
| b"foo" | "ไบต์" |
| [1, 2] | "array" |
| {"a": 1} | "map" |
path("c/d") |
"อ้างอิง" |
vector([1.0, 2.0]) |
"เวกเตอร์" |
| ABSENT | NULL |
ตัวอย่างลูกค้า
Node.js
const result = await db.pipeline() .collection("books") .select(field("title").notEqual("1984").as("not1984")) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select(field("title").notEqual("1984").as("not1984")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("title").notEqual("1984").as("not1984")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("title").notEqual("1984").alias("not1984")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("title").notEqual("1984").alias("not1984")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("title").not_equal("1984").as_("not1984")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(notEqual(field("title"), "1984").as("not1984")) .execute() .get();
IS_TYPE
ไวยากรณ์:
is_type(input: ANY, type: STRING) -> BOOLEAN
คำอธิบาย:
แสดงผล true หาก input ตรงกับ type ที่ระบุ หรือแสดงผล false หากไม่ตรงกัน
หากได้รับ input ที่ไม่มีอยู่ ฟังก์ชันจะแสดงผล NULL
สตริง type ที่รองรับมีดังนี้
"null""boolean""int32""int64""float64""decimal128""number""timestamp""string""bytes""array""map""reference""vector""geo_point""max_key""min_key""object_id""regex""bson_timestamp"
ตัวอย่าง
input |
type |
is_type(input, type) |
|---|---|---|
| NULL | "null" | จริง |
| จริง | "บูลีน" | จริง |
| 3.14 | "float64" | จริง |
| "foo" | "string" | จริง |
| b"foo" | "string" | เท็จ |
| [1, 2] | "array" | จริง |
| {"a": 1} | "map" | จริง |
vector([1.0, 2.0]) |
"เวกเตอร์" | จริง |
| ABSENT | "string" | NULL |
| "bar" | "อื่นๆ" | ข้อผิดพลาด |
ฟังก์ชันเวกเตอร์
| ชื่อ | คำอธิบาย |
COSINE_DISTANCE
|
แสดงผลระยะทางโคไซน์ระหว่างเวกเตอร์ 2 เวกเตอร์ |
DOT_PRODUCT
|
แสดงผลดอทโปรดักต์ระหว่าง 2 เวกเตอร์ |
EUCLIDEAN_DISTANCE
|
แสดงผลระยะทางแบบยุคลิดระหว่างเวกเตอร์ 2 ตัว |
MANHATTAN_DISTANCE
|
แสดงผลระยะทางแมนฮัตตันระหว่างเวกเตอร์ 2 เวกเตอร์ |
VECTOR_LENGTH
|
แสดงผลจำนวนองค์ประกอบในเวกเตอร์ |
COSINE_DISTANCE
ไวยากรณ์:
cosine_distance(x: VECTOR, y: VECTOR) -> FLOAT64
คำอธิบาย:
แสดงผลระยะทางโคไซน์ระหว่าง x กับ y
Node.js
const sampleVector = [0.0, 1, 2, 3, 4, 5]; const result = await db.pipeline() .collection("books") .select( field("embedding").cosineDistance(sampleVector).as("cosineDistance") ) .execute();
Web
const sampleVector = [0.0, 1, 2, 3, 4, 5]; const result = await execute(db.pipeline() .collection("books") .select( field("embedding").cosineDistance(sampleVector).as("cosineDistance")));
Swift
let sampleVector = [0.0, 1, 2, 3, 4, 5] let result = try await db.pipeline() .collection("books") .select([ Field("embedding").cosineDistance(sampleVector).as("cosineDistance") ]) .execute()
Kotlin
val sampleVector = doubleArrayOf(0.0, 1.0, 2.0, 3.0, 4.0, 5.0) val result = db.pipeline() .collection("books") .select( field("embedding").cosineDistance(sampleVector).alias("cosineDistance") ) .execute()
Java
double[] sampleVector = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("embedding").cosineDistance(sampleVector).alias("cosineDistance") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field from google.cloud.firestore_v1.vector import Vector sample_vector = Vector([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) result = ( client.pipeline() .collection("books") .select( Field.of("embedding").cosine_distance(sample_vector).as_("cosineDistance") ) .execute() )
Java
double[] sampleVector = new double[] {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(cosineDistance(field("embedding"), sampleVector).as("cosineDistance")) .execute() .get();
DOT_PRODUCT
ไวยากรณ์:
dot_product(x: VECTOR, y: VECTOR) -> FLOAT64
คำอธิบาย:
แสดงผลดอทโปรดักต์ของ x และ y
Node.js
const sampleVector = [0.0, 1, 2, 3, 4, 5]; const result = await db.pipeline() .collection("books") .select( field("embedding").dotProduct(sampleVector).as("dotProduct") ) .execute();
Web
const sampleVector = [0.0, 1, 2, 3, 4, 5]; const result = await execute(db.pipeline() .collection("books") .select( field("embedding").dotProduct(sampleVector).as("dotProduct") ) );
Swift
let sampleVector = [0.0, 1, 2, 3, 4, 5] let result = try await db.pipeline() .collection("books") .select([ Field("embedding").dotProduct(sampleVector).as("dotProduct") ]) .execute()
Kotlin
val sampleVector = doubleArrayOf(0.0, 1.0, 2.0, 3.0, 4.0, 5.0) val result = db.pipeline() .collection("books") .select( field("embedding").dotProduct(sampleVector).alias("dotProduct") ) .execute()
Java
double[] sampleVector = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("embedding").dotProduct(sampleVector).alias("dotProduct") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field from google.cloud.firestore_v1.vector import Vector sample_vector = Vector([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) result = ( client.pipeline() .collection("books") .select(Field.of("embedding").dot_product(sample_vector).as_("dotProduct")) .execute() )
Java
double[] sampleVector = new double[] {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(dotProduct(field("embedding"), sampleVector).as("dotProduct")) .execute() .get();
EUCLIDEAN_DISTANCE
ไวยากรณ์:
euclidean_distance(x: VECTOR, y: VECTOR) -> FLOAT64
คำอธิบาย:
คำนวณระยะทางแบบยุคลิดระหว่าง x กับ y
Node.js
const sampleVector = [0.0, 1, 2, 3, 4, 5]; const result = await db.pipeline() .collection("books") .select( field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") ) .execute();
Web
const sampleVector = [0.0, 1, 2, 3, 4, 5]; const result = await execute(db.pipeline() .collection("books") .select( field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") ) );
Swift
let sampleVector = [0.0, 1, 2, 3, 4, 5] let result = try await db.pipeline() .collection("books") .select([ Field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") ]) .execute()
Kotlin
val sampleVector = doubleArrayOf(0.0, 1.0, 2.0, 3.0, 4.0, 5.0) val result = db.pipeline() .collection("books") .select( field("embedding").euclideanDistance(sampleVector).alias("euclideanDistance") ) .execute()
Java
double[] sampleVector = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("embedding").euclideanDistance(sampleVector).alias("euclideanDistance") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field from google.cloud.firestore_v1.vector import Vector sample_vector = Vector([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) result = ( client.pipeline() .collection("books") .select( Field.of("embedding") .euclidean_distance(sample_vector) .as_("euclideanDistance") ) .execute() )
Java
double[] sampleVector = new double[] {0.0, 1.0, 2.0, 3.0, 4.0, 5.0}; Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(euclideanDistance(field("embedding"), sampleVector).as("euclideanDistance")) .execute() .get();
MANHATTAN_DISTANCE
ไวยากรณ์:
manhattan_distance(x: VECTOR, y: VECTOR) -> FLOAT64
คำอธิบาย:
คำนวณระยะทางแมนฮัตตันระหว่าง x กับ y
VECTOR_LENGTH
ไวยากรณ์:
vector_length(vector: VECTOR) -> INT64
คำอธิบาย:
แสดงผลจำนวนองค์ประกอบใน VECTOR
Node.js
const result = await db.pipeline() .collection("books") .select( field("embedding").vectorLength().as("vectorLength") ) .execute();
Web
const result = await execute(db.pipeline() .collection("books") .select( field("embedding").vectorLength().as("vectorLength") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("embedding").vectorLength().as("vectorLength") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("embedding").vectorLength().alias("vectorLength") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("embedding").vectorLength().alias("vectorLength") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("embedding").vector_length().as_("vectorLength")) .execute() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(vectorLength(field("embedding")).as("vectorLength")) .execute() .get();