Hàm chuỗi

Hàm chuỗi

Tên Mô tả
BYTE_LENGTH Trả về số lượng BYTES trong giá trị STRING hoặc BYTES
CHAR_LENGTH Trả về số lượng ký tự Unicode trong giá trị STRING
STARTS_WITH Trả về TRUE nếu STRING bắt đầu bằng một tiền tố nhất định
ENDS_WITH Trả về TRUE nếu STRING kết thúc bằng một hậu tố nhất định
LIKE Trả về TRUE nếu STRING khớp với một mẫu
REGEX_CONTAINS Trả về TRUE nếu một giá trị khớp một phần hoặc khớp hoàn toàn với một biểu thức chính quy
REGEX_MATCH Trả về TRUE nếu bất kỳ phần nào của một giá trị khớp với một biểu thức chính quy
STRING_CONCAT Nối nhiều STRING thành một STRING
STRING_CONTAINS Trả về TRUE nếu một giá trị chứa STRING
STRING_INDEX_OF Trả về chỉ mục (bắt đầu đếm từ 0) của lần xuất hiện đầu tiên của giá trị STRING hoặc BYTES.
TO_UPPER Chuyển đổi giá trị STRING hoặc BYTES thành chữ hoa.
TO_LOWER Chuyển đổi giá trị STRING hoặc BYTES thành chữ thường.
SUBSTRING Lấy một chuỗi con của giá trị STRING hoặc BYTES.
STRING_REVERSE Đảo ngược giá trị STRING hoặc BYTES.
STRING_REPEAT Lặp lại giá trị STRING hoặc BYTES theo một số lần được chỉ định.
STRING_REPLACE_ALL Thay thế tất cả các lần xuất hiện của giá trị STRING hoặc BYTES.
STRING_REPLACE_ONE Thay thế lần xuất hiện đầu tiên của giá trị STRING hoặc BYTES.
TRIM Cắt bỏ các ký tự ở đầu và cuối của giá trị STRING hoặc BYTES.
LTRIM Cắt bỏ các ký tự ở đầu khỏi giá trị STRING hoặc BYTES.
RTRIM Cắt bỏ các ký tự ở cuối giá trị STRING hoặc BYTES.
SPLIT Chia giá trị STRING hoặc BYTES thành một mảng.

BYTE_LENGTH

Cú pháp:

byte_length[T <: STRING | BYTES](value: T) -> INT64

Nội dung mô tả:

Trả về số lượng BYTES trong giá trị STRING hoặc BYTES.

Ví dụ:

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

Cú pháp:

char_length(value: STRING) -> INT64

Nội dung mô tả:

Trả về số lượng điểm mã Unicode trong giá trị STRING.

Ví dụ:

value char_length(value)
"abc" 3
"xin chào" 5
"thế giới" 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

Cú pháp:

starts_with(value: STRING, prefix: STRING) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu value bắt đầu bằng prefix.

Ví dụ:

value tiền tố starts_with(value, prefix)
"abc" "a" đúng
"abc" "b" false
"abc" "" đúng
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

Cú pháp:

ends_with(value: STRING, postfix: STRING) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu value kết thúc bằng postfix.

Ví dụ:

value postfix ends_with(value, postfix)
"abc" "c" đúng
"abc" "b" false
"abc" "" đúng
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();

THÍCH

Cú pháp:

like(value: STRING, pattern: STRING) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu value khớp với pattern.

Ví dụ:

value hoa văn like(value, pattern)
"Firestore" "Fire%" đúng
"Firestore" "%store" đúng
"Datastore" "Data_tore" đúng
"100%" "100%" đúng
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

Cú pháp:

regex_contains(value: STRING, pattern: STRING) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu một phần của value khớp với pattern. Nếu pattern không phải là một biểu thức chính quy hợp lệ, thì hàm này sẽ trả về error.

Biểu thức chính quy tuân theo cú pháp của thư viện re2.

Ví dụ:

value hoa văn regex_contains(value, pattern)
"Firestore" "Lửa" đúng
"Firestore" "store$" đúng
"Firestore" "data" false
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

Cú pháp:

regex_match(value: STRING, pattern: STRING) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu value hoàn toàn khớp với pattern. Nếu pattern không phải là một biểu thức chính quy hợp lệ, thì hàm này sẽ trả về error.

Biểu thức chính quy tuân theo cú pháp của thư viện re2.

Ví dụ:

value hoa văn regex_match(value, pattern)
"Firestore" "F.*store" đúng
"Firestore" "Lửa" false
"Firestore" "^F.*e$" đúng
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

Cú pháp:

string_concat(values: STRING...) -> STRING

Nội dung mô tả:

Nối hai hoặc nhiều giá trị STRING thành một kết quả duy nhất.

Ví dụ:

đối số string_concat(values...)
() error
("a") "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

Cú pháp:

string_contains(value: STRING, substring: STRING) -> BOOLEAN

Nội dung mô tả:

Kiểm tra xem value có chứa chuỗi ký tự substring hay không.

Ví dụ:

value chuỗi con string_contains(value, substring)
"abc" "b" đúng
"abc" "d" false
"abc" "" đúng
"a.c" "." đúng
"☃☃☃" "☃" đúng
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

Cú pháp:

string_index_of[T <: STRING | BYTES](value: T, search: T) -> INT64

Nội dung mô tả:

Trả về chỉ mục dựa trên 0 của lần xuất hiện đầu tiên của search trong value.

  • Trả về -1 nếu không tìm thấy search.
  • Nếu value là giá trị STRING, thì kết quả sẽ được đo bằng điểm mã unicode. Nếu là giá trị BYTES, thì giá trị này được đo bằng byte.
  • Nếu search là một giá trị STRING hoặc BYTES trống, kết quả sẽ là 0.

Ví dụ:

value tìm-kiếm string_index_of(value, search)
"hello world" "o" 4
"hello world" "l" 2
"hello world" "z" -1
"chuối" "na" 2
"abc" "" 0
b"abc" b"b" 1
"é" "é" 0
b"é" b"é" 0

TO_UPPER

Cú pháp:

to_upper[T <: STRING | BYTES](value: T) -> T

Nội dung mô tả:

Chuyển đổi giá trị STRING hoặc BYTES thành chữ hoa.

Nếu một byte hoặc ký tự không tương ứng với một ký tự chữ cái viết thường UTF-8, thì byte hoặc ký tự đó sẽ được truyền qua mà không thay đổi.

Ví dụ:

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();

Hàm TO_LOWER

Cú pháp:

to_lower[T <: STRING | BYTES](value: T) -> T

Nội dung mô tả:

Chuyển đổi giá trị STRING hoặc BYTES thành chữ thường.

Nếu một byte hoặc ký tự không tương ứng với một ký tự chữ cái viết hoa UTF-8, thì byte hoặc ký tự đó sẽ được truyền qua mà không thay đổi.

Ví dụ:

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

Cú pháp:

substring[T <: STRING | BYTES](input: T, position: INT64) -> T
substring[T <: STRING | BYTES](input: T, position: INT64, length: INT64) -> T

Nội dung mô tả:

Trả về một chuỗi con của input, bắt đầu từ position (chỉ mục dựa trên 0) và bao gồm tối đa length mục. Nếu không có length, hãy trả về chuỗi con từ position đến cuối input.

  • Nếu input là giá trị STRING, thì positionlength sẽ được đo bằng điểm mã unicode. Nếu đó là giá trị BYTES, thì các giá trị này được đo bằng byte.

  • Nếu position lớn hơn độ dài của input, thì hàm sẽ trả về một chuỗi con trống. Nếu position cộng với length lớn hơn độ dài của input, thì chuỗi con sẽ bị cắt bớt đến cuối input.

  • Nếu position là số âm, vị trí sẽ được lấy từ cuối dữ liệu đầu vào. Nếu giá trị âm position lớn hơn kích thước của dữ liệu đầu vào, thì vị trí sẽ được đặt thành 0. length không được là số âm.

Ví dụ:

Khi bạn không cung cấp length:

nhập liệu position substring(input, position)
"abc" 0 "abc"
"abc" 1 "bc"
"abc" 3 ""
"abc" -1 "c"
b"abc" 1 b"bc"

Khi bạn cung cấp length:

nhập liệu position chiều dài substring(input, position, length)
"abc" 0 1 "a"
"abc" 1 2 "bc"
"abc" -1 1 "c"
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

Cú pháp:

string_reverse[T <: STRING | BYTES](input: T) -> T

Nội dung mô tả:

Trả về dữ liệu đầu vào đã cung cấp theo thứ tự đảo ngược.

Các ký tự được phân định bằng điểm mã Unicode khi đầu vào là STRING và bằng byte khi đầu vào là giá trị BYTES.

Ví dụ:

nhập liệu string_reverse(input)
"abc" "cba"
"a🌹b" "b🌹a"
"xin chào" "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

Cú pháp:

string_repeat[T <: STRING | BYTES](input: T, repetitions: INT64) -> T

Nội dung mô tả:

Trả về input được lặp lại repetitions lần.

  • repetitions phải là một số nguyên không âm.
  • Nếu repetitions0, hàm sẽ trả về một giá trị trống có cùng loại với input.
  • Nếu kết quả vượt quá kích thước tối đa cho phép (1 MB), thì hệ thống sẽ trả về lỗi.

Ví dụ:

nhập liệu số lần lặp lại string_repeat(input, repetitions)
"foo" 3 "foofoofoo"
"foo" 0 ""
"a " 3 "a a a "
b"ab" 2 b"abab"
"é🦆" 2 "é🦆é🦆"

STRING_REPLACE_ALL

Cú pháp:

string_replace_all[T <: STRING | BYTES](input: T, find: T, replacement: T) -> T

Nội dung mô tả:

Thay thế tất cả các lần xuất hiện không trùng lặp của find trong input bằng replacement.

  • Các kết quả trùng khớp có phân biệt chữ hoa chữ thường.
  • Nếu find trống, thì sẽ không có nội dung nào được thay thế.

Ví dụ:

nhập liệu tìm thay thế string_replace_all(input, find, replacement)
"foobarfoo" "foo" "baz" "bazbarbaz"
"ababab" "aba" "c" "cbab"
"foobar" "o" "" "fbar"
"é🦆🌎🦆" "🦆" "a" "éa🌎a"
b"abc" b"b" b"d" b"adc"

STRING_REPLACE_ONE

Cú pháp:

string_replace_one[T <: STRING | BYTES](input: T, find: T, replacement: T) -> T

Nội dung mô tả:

Thay thế lần xuất hiện đầu tiên của find trong input bằng replacement.

  • Các kết quả trùng khớp có phân biệt chữ hoa chữ thường.
  • Nếu find trống, thì sẽ không có nội dung nào được thay thế.

Ví dụ:

nhập liệu tìm thay thế string_replace_one(input, find, replacement)
"foobarfoo" "foo" "baz" "bazbarfoo"
"é" "é" "a" "a"
b"foobar" b"o" b"z" b"fzoobar"

TRIM

Cú pháp:

trim[T <: STRING | BYTES](input: T, values_to_trim: T) -> T
trim[T <: STRING | BYTES](input: T) -> T

Nội dung mô tả:

Cắt một nhóm BYTES hoặc CHARS đã chỉ định ở đầu và cuối input được cung cấp.

  • Nếu không có values_to_trim nào được cung cấp, hãy cắt bỏ các ký tự khoảng trắng.

Ví dụ:

Khi bạn không cung cấp values_to_trim:

nhập liệu 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"

Khi bạn cung cấp values_to_trim:

nhập liệu values_to_trim trim(input, values_to_trim)
"abcbfooaacb" "abc" "foo"
"abcdaabadbac" "abc" "daabad"
b"C1C2C3" b"C1" b"C2C3"
b"C1C2" "foo" error
"foo" b"C1" error

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();

Hàm LTRIM

Cú pháp:

ltrim[T <: STRING | BYTES](value: T, to_trim: T) -> T
ltrim[T <: STRING | BYTES](value: T) -> T

Nội dung mô tả:

Cắt bỏ một tập hợp BYTES hoặc CHARS được chỉ định ở đầu value được cung cấp.

  • Nếu bạn không cung cấp to_trim, thì các ký tự khoảng trắng ở đầu sẽ bị cắt bớt.

Ví dụ:

Khi bạn không cung cấp to_trim:

value ltrim(value)
" foo " "foo "
"foo" "foo"

Khi bạn cung cấp to_trim:

value to_trim ltrim(value, to_trim)
"aaabc" "a" "bc"
"abacaba" "ba" "caba"
"é" "é" ""

RTRIM

Cú pháp:

rtrim[T <: STRING | BYTES](value: T, to_trim: T) -> T
rtrim[T <: STRING | BYTES](value: T) -> T

Nội dung mô tả:

Cắt một nhóm BYTES hoặc CHARS đã chỉ định khỏi phần cuối của value được cung cấp.

  • Nếu bạn không cung cấp to_trim, thì hàm này sẽ cắt bỏ các ký tự khoảng trắng ở cuối.

Ví dụ:

Khi bạn không cung cấp to_trim:

value rtrim(value)
" foo " " foo"
"foo" "foo"

Khi bạn cung cấp to_trim:

value to_trim rtrim(value, to_trim)
"abccc" "c" "ab"
"abacaba" "ba" "abac"
"é" "é" ""

SPLIT

Cú pháp:

split(input: STRING) -> ARRAY<STRING>
split[T <: STRING | BYTES](input: T, delimiter: T) -> ARRAY<T>

Nội dung mô tả:

Chia giá trị STRING hoặc BYTES bằng dấu phân cách.

  • Đối với STRING, dấu phân cách mặc định là dấu phẩy ,. Dấu phân cách được coi là một chuỗi duy nhất.

  • Đối với BYTES, bạn phải chỉ định một dấu phân cách.

  • Việc phân chia bằng dấu phân cách trống sẽ tạo ra một mảng các điểm mã Unicode cho các giá trị STRING và một mảng BYTES cho các giá trị BYTES.

  • Việc phân chia một STRING trống sẽ trả về một ARRAY có một STRING trống.

Ví dụ:

Khi bạn không cung cấp delimiter:

nhập liệu split(input)
"foo,bar,foo" ["foo", "bar", "foo"]
"foo" ["foo"]
",foo," ["", "foo", ""]
"" [""]
b"C120C2C4" error

Khi bạn cung cấp delimiter:

nhập liệu dấu phân cách 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" error