Hàm so sánh

Hàm so sánh

Tên Mô tả
EQUAL So sánh về sự bằng nhau
GREATER_THAN So sánh lớn hơn
GREATER_THAN_OR_EQUAL So sánh lớn hơn hoặc bằng
LESS_THAN So sánh nhỏ hơn
LESS_THAN_OR_EQUAL So sánh nhỏ hơn hoặc bằng
NOT_EQUAL So sánh không bằng

EQUAL

Cú pháp:

equal(x: ANY, y: ANY) -> BOOLEAN

Ví dụ:

x y equal(x, y)
1L 1L TRUE
1.0 1L TRUE
-1 1L FALSE
NaN (Không phải số) NaN (Không phải số) TRUE
NULL NULL TRUE
NULL ABSENT FALSE

Nội dung mô tả:

Trả về TRUE nếu xy bằng nhau, nếu không thì trả về FALSE.

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

GREATER_THAN

Cú pháp:

greater_than(x: ANY, y: ANY) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu x lớn hơn y, và FALSE nếu ngược lại.

Nếu xy không so sánh được, hàm sẽ trả về FALSE.

Ví dụ:

x y greater_than(x, y)
1L 0.0 TRUE
1L 1L FALSE
1L 2L FALSE
"foo" 0L FALSE
0L "foo" FALSE
NaN (Không phải số) 0L FALSE
0L NaN (Không phải số) FALSE
NULL NULL FALSE

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

GREATER_THAN_OR_EQUAL

Cú pháp:

greater_than_or_equal(x: ANY, y: ANY) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu x lớn hơn hoặc bằng y, và FALSE nếu ngược lại.

Nếu xy không so sánh được, hàm sẽ trả về FALSE.

Ví dụ:

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 (Không phải số) 0L FALSE
0L NaN (Không phải số) FALSE
NULL NULL TRUE

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

LESS_THAN

Cú pháp:

less_than(x: ANY, y: ANY) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu x nhỏ hơn y, và FALSE nếu ngược lại.

Nếu xy không so sánh được, hàm sẽ trả về FALSE.

Ví dụ:

x y less_than(x, y)
1L 0.0 FALSE
1L 1L FALSE
1L 2L TRUE
"foo" 0L FALSE
0L "foo" FALSE
NaN (Không phải số) 0L FALSE
0L NaN (Không phải số) FALSE
NULL NULL FALSE

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

LESS_THAN_OR_EQUAL

Cú pháp:

less_than_or_equal(x: ANY, y: ANY) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu x nhỏ hơn hoặc bằng y, và FALSE nếu ngược lại.

Nếu xy không so sánh được, hàm sẽ trả về FALSE.

Ví dụ:

x y less_than(x, y)
1L 0.0 FALSE
1L 1L TRUE
1L 2L TRUE
"foo" 0L FALSE
0L "foo" FALSE
NaN (Không phải số) 0L FALSE
0L NaN (Không phải số) FALSE
NULL NULL TRUE

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

NOT_EQUAL

Cú pháp:

not_equal(x: ANY, y: ANY) -> BOOLEAN

Nội dung mô tả:

Trả về TRUE nếu x không bằng y, và FALSE nếu ngược lại.

Ví dụ:

x y not_equal(x, y)
1L 1L FALSE
1.0 1L FALSE
-1 1L TRUE
NaN (Không phải số) 0L TRUE
NaN (Không phải số) NaN (Không phải số) FALSE
NULL NULL FALSE
NULL ABSENT TRUE

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