Starts-With Query
-
Use
whereStartsWith
to query for documents having a field that starts with some specified string. -
Unless explicitly specified, the string comparisons are case-insensitive by default.
-
In this page:
whereStartsWith
const products = await session
.query({ collection: "Products" })
// Call 'whereStartsWith'
// Pass the document field and the prefix to search by
.whereStartsWith("Name", "Ch")
.all();
// Results will contain only Product documents having a 'Name' field
// that starts with any case variation of 'ch'
from "Products"
where startsWith(Name, "Ch")
whereStartsWith (case-sensitive)
const products = await session
.query({ collection: "Products" })
// Call 'whereStartsWith'
// Pass 'true' as the 3'rd parameter to search for an EXACT prefix match
.whereStartsWith("Name", "Ch", true)
.all();
// Results will contain only Product documents having a 'Name' field
// that starts with 'Ch'
from "Products"
where exact(startsWith(Name, "Ch"))
Negate whereStartsWith
const products = await session
.query({ collection: "Products" })
// Call 'Not' to negate the next predicate
.not()
// Call 'whereStartsWith'
// Pass the document field and the prefix to search by
.whereStartsWith("Name", "Ch")
.all();
// Results will contain only Product documents having a 'Name' field
// that does NOT start with 'ch' or any other case variations of it
from "Products"
where exists(Name) and not startsWith(Name, "Ch")
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 prefix string to search by |
exact | boolean | false - search is case-insensitivetrue - search is case-sensitive |