filter method allows you to
restrict the documents that your document query returns. This method takes a
filter constructed by FilterBuilder
and will only select documents that match.
The examples below demonstrate some of the common uses of filter. You can see
the full list of available filtering methods
in the reference docs.
If you need to filter to documents containing some keywords, use a
search query.
Equality conditions
This document query finds documents in theusers table where
doc.name === "Alex":
q is the FilterBuilder utility
object. It contains methods for all of our supported filter operators.
This filter will run on all documents in the table. For each document,
q.field("name") evaluates to the name property. Then q.eq checks if this
property is equal to "Alex".
If your query references a field that is missing from a given document then that
field will be considered to have the value undefined.
Comparisons
Filters can also be used to compare fields against values. This document query finds documents wheredoc.age >= 18:
q.gte operator checks if the first argument (doc.age) is greater
than or equal to the second (18).
Here’s the full list of comparisons:
Arithmetic
You can also include basic arithmetic in your queries. This document query finds documents in thecarpets table where doc.height * doc.width > 100:
Combining operators
You can construct more complex filters using methods likeq.and, q.or, and
q.not. This document query finds documents where
doc.name === "Alex" && doc.age >= 18:
doc.name === "Alex" || doc.name === "Emma":
Advanced filtering techniques
Sometimes the filter syntax is is not expressive enough. For example you may want to collect all posts that have a tag. Your schema for the posts looks like this:collect()
call. This is just filtering a JavaScript array:
for await...of
syntax to loop through the table one document at a time:
Querying performance and limits
Most of the example document queries above can lead to a full table scan. That is, for the document query to return the requested results, it might need to walk over every single document in the table. Take this simple example:"tasks" table just to
find the first one with isCompleted: true.
If a table has more than a few thousand documents, you should use
indexes to improve your document
query performance. Otherwise, you may run into our enforced limits, detailed in
Read/write limit errors.
For information on other limits, see Limits.