Ends-With Query


  • Use whereEndsWith to query for documents having a field that ends with some specified string.

  • Unless explicitly specified, the string comparisons are case-insensitive by default.

  • Note:
    This postfix search causes the server to perform a full index scan.
    Instead, consider using a static index that indexes the field in reverse order
    and then query with a prefix search, which is much faster.

  • In this page:


whereEndsWith

const products = await session
    .query({ collection: "Products" })
     // Call 'whereEndsWith'
     // Pass the document field and the postfix to search by
    .whereEndsWith("Name", "Lager")
    .all();

// Results will contain only Product documents having a 'Name' field
// that ends with any case variation of 'lager'
from "Products"
where endsWith(Name, "Lager")

whereEndsWith (case-sensitive)

const products = await session
    .query({ collection: "Products" })
    // Call 'whereEndsWith'
    // Pass 'true' as the 3'rd parameter to search for an EXACT postfix match
    .whereEndsWith("Name", "Lager", true)
    .all();

// Results will contain only Product documents having a 'Name' field
// that ends with 'Lager'
from "Products"
where exact(endsWith(Name, "Lager"))

Negate whereEndsWith

const products = await session
    .query({ collection: "Products" })
    // Call 'Not' to negate the next predicate
    .not()
    // Call 'whereEndWith'
    // Pass the document field and the postfix to search by
    .whereEndsWith("Name", "Lager")
    .all();

// Results will contain only Product documents having a 'Name' field
// that does NOT end with 'lager' or any other case variations of it
from "Products"
where exists(Name) and not endsWith(Name, "Lager")

Syntax

// Available overloads:
whereStartsWith(fieldName, value);
whereStartsWith(fieldName, value, exact);
Parameter Type Description
fieldName string The field name in which to search
value string The postfix string to search by
exact boolean false - search is case-insensitive
true - search is case-sensitive