query
To query a collection, use the query
method on Collection
:
It accepts a function as the argument, where you define the query.
The method returns an array of Doc
instances:
Builder mode
The query
also allows you to use the builder mode, where you accumulate the query operations and then call run
to execute them:
Subscription
Instead of awaiting the promise returned from query
, you can call on
on it to subscribe to the document updates:
To catch errors, use catch
after calling on
:
To stop listening to the updates, call the off
function returned from the method:
You can also subscribe to query in the builder mode:
→ Read more about subscribing to real-time updates
Pagination
To paginate data, use one of the methods provided by the $
helper:
Use last user id, starting with undefined
, to query the page, $.limit
to specify the page size.
Aggregation queries
You can use queries to aggregate data in a collection. There are three methods available: count
, sum
, and average
.
count
You can count the documents in a collection by calling the count
method:
The method returns Promise<number>
.
sum
You can sum the document fields in a collection by calling the sum
method:
The method returns Promise<number>
.
average
You can calculate the average of the document fields in a collection by calling the average
method:
The method returns Promise<number>
.
$
helper
The argument function receives $
helper object as the first argument that provides the query API.
$
type is TypesaurusQuery.Helpers
.
You can return either a single query, array of queries or falsy value to skip the query and return undefined
:
$.field
The $.field
allows you to target specific fields in the document. It allows you to query nested fields as well:
The result of the $.field
provides a few methods that define the query:
$.field(...).eq
- field is equal to a value$.field(...).not
- field is not equal to a value$.field(...).lt
- field is less than a value$.field(...).lte
- field is less or equal to a value$.field(...).gt
- field is greater than a value$.field(...).gte
- field is greater or equal to a value$.field(...).in
- field is in an array of values$.field(...).notIn
- field is not in an array of values$.field(...).contains
- field array contains a value$.field(...).containsAny
- field array contains any of the values$.field(...).order
- order the query by the field
$.field(...).eq
To query documents where the field is equal to a value, use the eq
method:
$.field(...).not
To query documents where the field is not equal to a value, use the not
method:
$.field(...).lt
To query documents where the field is less than a value, use the lt
method:
The method only works with numbers and dates.
$.field(...).lte
To query documents where the field is less or equal to a value, use the lte
method:
The method only works with numbers and dates.
$.field(...).gt
To query documents where the field is greater than a value, use the gt
method:
The method only works with numbers and dates.
$.field(...).gte
To query documents where the field is greater or equal to a value, use the gte
method:
The method only works with numbers and dates.
$.field(...).in
To query documents where the field is in an array of values, use the in
method:
$.field(...).notIn
To query documents where the field is not in an array of values, use the notIn
method:
$.field(...).contains
To query documents where the field array contains a value, use the contains
method:
The method only works with arrays.
$.field(...).containsAny
To query documents where the field array contains any of the values, use the containsAny
method:
The method only works with arrays.
$.field(...).order
To order the query by the field, use the order
method:
It accepts the optional direction ("asc" | "desc"
) and optionally the range of values to order by. You can call the method without any arguments to order by the field in ascending order:
The field helpers that can be used to define the range are:
$.startAt
- start the range at a value$.startAfter
- start the range after a value$.endAt
- end the range at a value$.endBefore
- end the range before a value
$.or
To query documents that match any of the queries, use the or
method:
All the rest queries are considered to be joined by the and operator.
$.limit
To limit the number of documents returned by the query, use the limit
method:
$.startAt
To start the range at a value, use the startAt
method:
$.startAfter
To start the range after a value, use the startAfter
method:
$.endAt
To end the range at a value, use the endAt
method:
$.endBefore
To end the range before a value, use the endBefore
method:
$.docId
To use the document id in the query, use the $.docId
method:
It effectively allows you to paginate the query.
Options
as
You can tell Typesaurus that it’s safe to use dates by passing the as
option ("server" | "client"
):
By default, Typesaurus uses the "client"
option.
The builder mode also accepts the as
option: