توابع منطقی

توابع منطقی

نام توضیحات
AND یک AND منطقی انجام می‌دهد
OR یک OR منطقی انجام می‌دهد
XOR یک XOR منطقی انجام می‌دهد
NOT یک NOT منطقی انجام می‌دهد.
NOR یک NOR منطقی انجام می‌دهد.
CONDITIONAL ارزیابی شاخه‌ها بر اساس یک عبارت شرطی.
IF_NULL اولین مقدار غیر تهی را برمی‌گرداند
SWITCH_ON ارزیابی شعب بر اساس مجموعه‌ای از شرایط
EQUAL_ANY بررسی می‌کند که آیا یک مقدار با هر عنصری در یک آرایه برابر است یا خیر.
NOT_EQUAL_ANY بررسی می‌کند که آیا یک مقدار با هیچ یک از عناصر آرایه برابر نیست یا خیر.
MAXIMUM حداکثر مقدار را در مجموعه‌ای از مقادیر برمی‌گرداند
MINIMUM کمترین مقدار را در مجموعه‌ای از مقادیر برمی‌گرداند

و

نحو:

and(x: BOOLEAN...) -> BOOLEAN

شرح:

عملگر منطقی AND را برای دو یا چند مقدار بولی برمی‌گرداند.

اگر نتیجه به دلیل ABSENT یا NULL بودن هر یک از مقادیر داده شده قابل استخراج نباشد، NULL را برمی‌گرداند.

مثال‌ها:

x y and(x, y)
TRUE TRUE TRUE
FALSE TRUE FALSE
NULL TRUE NULL
ABSENT TRUE NULL
NULL FALSE FALSE
FALSE ABSENT FALSE
نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
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 منطقی دو یا چند مقدار بولی را برمی‌گرداند.

اگر نتیجه به دلیل ABSENT یا NULL بودن هر یک از مقادیر داده شده قابل استخراج نباشد، NULL را برمی‌گرداند.

مثال‌ها:

x y or(x, y)
TRUE TRUE TRUE
FALSE TRUE TRUE
NULL TRUE TRUE
ABSENT TRUE TRUE
NULL FALSE NULL
FALSE ABSENT NULL
نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
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 منطقی دو یا چند مقدار بولی را برمی‌گرداند.

اگر هر یک از مقادیر داده شده ABSENT یا NULL باشند 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
نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .select(
            xor(
                    arrayContains(field("tags"), "magic"),
                    arrayContains(field("tags"), "nonfiction"))
                .as("matchesSearchFilters"))
        .execute()
        .get();

نه

نحو:

nor(x: BOOLEAN...) -> BOOLEAN

شرح:

تابع NOR منطقی دو یا چند مقدار بولی را برمی‌گرداند.

اگر نتیجه به دلیل ABSENT یا NULL بودن هر یک از مقادیر داده شده قابل استخراج نباشد، 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(x: BOOLEAN) -> BOOLEAN

شرح:

مقدار منطقی NOT یک مقدار بولی را برمی‌گرداند.

نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .select(not(arrayContains(field("tags"), "nonfiction")).as("isFiction"))
        .execute()
        .get();

مشروط

نحو:

conditional(condition: BOOLEAN, true_case: ANY, false_case: ANY) -> ANY

شرح:

اگر condition TRUE باشد، true_case را ارزیابی کرده و برمی‌گرداند.

اگر شرط به FALSE ، NULL یا مقدار ABSENT ختم شود، false_case ارزیابی کرده و برمی‌گرداند.

مثال‌ها:

condition true_case false_case conditional(condition, true_case, false_case)
TRUE ۱ لیتر 0 لیتر ۱ لیتر
FALSE ۱ لیتر 0 لیتر 0 لیتر
NULL ۱ لیتر 0 لیتر 0 لیتر
ABSENT ۱ لیتر 0 لیتر 0 لیتر
نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .select(
            arrayConcat(
                    field("tags"),
                    conditional(
                        greaterThan(field("pages"), 100),
                        constant("longRead"),
                        constant("shortRead")))
                .as("extendedTags"))
        .execute()
        .get();

اگر_NULL

نحو:

if_null(expr: ANY, replacement: ANY) -> ANY

شرح:

اگر NULL نباشد، expr برمی‌گرداند، در غیر این صورت ارزیابی کرده و replacement را برمی‌گرداند. اگر expr استفاده شود، عبارت replacement ارزیابی نمی‌شود.

مثال‌ها:

expr replacement if_null(expr, replacement)
۱ لیتر ۲ لیتر ۱ لیتر
NULL ۲ لیتر ۲ لیتر
ABSENT ۲ لیتر 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")
۱ لیتر «یک»
۲ لیتر «دو»
۳ لیتر «دیگر»

مساوی_هر

نحو:

equal_any(value: ANY, search_space: ARRAY) -> BOOLEAN

شرح:

اگر value در آرایه search_space باشد، TRUE برمی‌گرداند.

مثال‌ها:

value search_space equal_any(value, search_space)
0 لیتر [۱ لیتر، ۲ لیتر، ۳ لیتر] FALSE
۲ لیتر [۱ لیتر، ۲ لیتر، ۳ لیتر] TRUE
NULL [۱ لیتر، ۲ لیتر، ۳ لیتر] FALSE
NULL [1L، NULL ] TRUE
ABSENT [1L، NULL ] FALSE
نان ن [1 لیتر، NaN، 3 لیتر] TRUE
نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .select(
            equalAny(field("genre"), Arrays.asList("Science Fiction", "Psychological Thriller"))
                .as("matchesGenreFilters"))
        .execute()
        .get();

برابر نیست

نحو:

not_equal_any(value: ANY, search_space: ARRAY) -> BOOLEAN

شرح:

اگر value در آرایه search_space نباشد، TRUE برمی‌گرداند.

مثال‌ها:

value search_space not_equal_any(value, search_space)
0 لیتر [۱ لیتر، ۲ لیتر، ۳ لیتر] TRUE
۲ لیتر [۱ لیتر، ۲ لیتر، ۳ لیتر] FALSE
NULL [۱ لیتر، ۲ لیتر، ۳ لیتر] TRUE
NULL [1L، NULL ] FALSE
ABSENT [1L، NULL ] TRUE
نان ن [1 لیتر، NaN، 3 لیتر] FALSE
نود جی اس
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")
  )
);
سویفت
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();
پایتون
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()
)
جاوا
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 -10 لیتر -10 لیتر
۰.۰ -5 لیتر ۰.۰
"فو" "بار" "فو"
"فو" ["غذا"] ["غذا"]
ABSENT ABSENT NULL
NULL NULL NULL
نود جی اس
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"))
);
سویفت
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();
پایتون
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("rating").logical_maximum(1).as_("flooredRating"))
    .execute()
)
جاوا
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 -10 لیتر FALSE
۰.۰ -5 لیتر -5 لیتر
"فو" "بار" "بار"
"فو" ["غذا"] "فو"
ABSENT ABSENT NULL
NULL NULL NULL
نود جی اس
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"))
);
سویفت
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();
پایتون
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("rating").logical_minimum(5).as_("cappedRating"))
    .execute()
)
جاوا
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .select(logicalMinimum(field("rating"), 5).as("cappedRating"))
        .execute()
        .get();