FirebaseFirestore Framework Reference

Functions

The following functions are available globally.

  • Combines two boolean expressions with a logical AND (&&).

    The resulting expression is true only if both the left-hand side (lhs) and the right-hand side (rhs) are true.

    // Find books in the "Fantasy" genre with a rating greater than 4.5
    firestore.pipeline()
      .collection("books")
      .where(
        Field("genre").equal("Fantasy") && Field("rating").greaterThan(4.5)
      )
    

    Declaration

    Swift

    public func && (lhs: BooleanExpression,
                    rhs: @autoclosure () throws -> BooleanExpression) rethrows -> BooleanExpression

    Parameters

    lhs

    The left-hand boolean expression.

    rhs

    The right-hand boolean expression.

    Return Value

    A new BooleanExpression representing the logical AND.

  • Combines two boolean expressions with a logical OR (||).

    The resulting expression is true if either the left-hand side (lhs) or the right-hand side (rhs) is true.

    // Find books that are either in the "Romance" genre or were published before 1900
    firestore.pipeline()
      .collection("books")
      .where(
        Field("genre").equal("Romance") || Field("published").lessThan(1900)
      )
    

    Declaration

    Swift

    public func || (lhs: BooleanExpression,
                    rhs: @autoclosure () throws -> BooleanExpression) rethrows -> BooleanExpression

    Parameters

    lhs

    The left-hand boolean expression.

    rhs

    The right-hand boolean expression.

    Return Value

    A new BooleanExpression representing the logical OR.

  • Combines two boolean expressions with a logical XOR (^).

    The resulting expression is true if the left-hand side (lhs) and the right-hand side (rhs) have different boolean values.

    // Find books that are in the "Dystopian" genre OR have a rating of 5.0, but not both.
    firestore.pipeline()
      .collection("books")
      .where(
        Field("genre").equal("Dystopian") ^ Field("rating").equal(5.0)
      )
    

    Declaration

    Swift

    public func ^ (lhs: BooleanExpression,
                   rhs: @autoclosure () throws -> BooleanExpression) rethrows -> BooleanExpression

    Parameters

    lhs

    The left-hand boolean expression.

    rhs

    The right-hand boolean expression.

    Return Value

    A new BooleanExpression representing the logical XOR.

  • Negates a boolean expression with a logical NOT (!).

    The resulting expression is true if the original expression is false, and vice versa.

    // Find books that are NOT in the "Science Fiction" genre
    firestore.pipeline()
      .collection("books")
      .where(!Field("genre").equal("Science Fiction"))
    

    Declaration

    Swift

    public prefix func ! (lhs: BooleanExpression) -> BooleanExpression

    Parameters

    lhs

    The boolean expression to negate.

    Return Value

    A new BooleanExpression representing the logical NOT.

  • Combines given boolean expressions with a logical NOR (nor).

    The resulting expression is true only if all of the provided expressions are false.

    // Find books that are neither "Fantasy" nor have a rating > 4.5
    firestore.pipeline()
      .collection("books")
      .where(nor(Field("genre").equal("Fantasy"), Field("rating").greaterThan(4.5)))
    

    Declaration

    Swift

    public func nor(_ condition: BooleanExpression,
                    _ conditions: BooleanExpression...) -> BooleanExpression

    Parameters

    condition

    The first boolean expression.

    conditions

    Additional boolean expressions.

    Return Value

    A new BooleanExpression representing the logical NOR.

  • Combines given boolean expressions with a logical NOR (nor).

    The resulting expression is true only if all of the provided expressions are false.

    // Find books that are neither "Fantasy" nor have a rating > 4.5
    let conditions: [BooleanExpression] = [
      Field("genre").equal("Fantasy"),
      Field("rating").greaterThan(4.5)
    ]
    firestore.pipeline()
      .collection("books")
      .where(nor(conditions))
    

    Declaration

    Swift

    public func nor(_ conditions: [BooleanExpression]) -> BooleanExpression

    Parameters

    conditions

    An array of boolean expressions. Must contain at least 2 expressions.

    Return Value

    A new BooleanExpression representing the logical NOR.

  • // Create a "sizeCategory" field based on the "amount" field.
    firestore.pipeline()
      .collection("items")
      .addFields([
        switchOn(
          Field("amount").lessThan(10), Constant("Small"),
          Field("amount").lessThan(100), Constant("Medium"),
          Constant("Large")
        ).as("sizeCategory")
      ])
    

    Declaration

    Swift

    public func switchOn(_ condition: BooleanExpression,
                         _ result: Expression,
                         _ others: Expression...) -> Expression

    Parameters

    condition

    The first condition to evaluate.

    result

    The expression to return if the first condition is true.

    others

    Additional condition/result pairs, optionally followed by a default expression.

    Return Value

    A new Expression representing the switchOn logic.

  • Creates an expression that generates a random number between 0.0 and 1.0 but not including 1.0.

    // Generate a random number between 0.0 and 1.0.
    rand()
    

    Declaration

    Swift

    public func rand() -> RandomExpression

    Return Value

    A new RandomExpression representing the random number generator.