Query Documents
Tigris provides powerful query functionality for specifying which documents you want to retrieve. There is no need to index any field as Tigris allows querying on any field of a document.
Specifically:
- Filter to filter queries on collections
- Sort to sort collection queries
- Pagination to paginate through query results
Filterβ
Filter provides the following comparison operators with the same semantics as in virtually all programming languages.
- filter.Eq: equal to is used for exact matching.
- filter.Lt: less than is used for matching documents using less than criteria.
- filter.Lte: less than or equal to is similar to filter.Lt but also matches for equality.
- filter.Gt: greater than is used for matching documents using greater than criteria.
- filter.Gte: greater than or equal to is similar to filter.Gt but also matches for equality.
For multiple conditions, there are two logical operators supported.
- filter.Or: Combines multiple filter operators and returns documents when either condition is met.
- filter.And: Combines multiple filter operators and returns documents when all the conditions are met.
Filter examplesβ
The first step is to create the collection object.
catalogs := tigris.GetCollection[Catalog](catalogdb)
Assuming an e-commerce website that has the above collection catalog and has 5 products(documents) in it.
id | name | price | brand | labels | popularity | reviews |
---|---|---|---|---|---|---|
1 | fiona handbag | 99.9 | michael kors | purses | 8 | {"author": "alice", "rating": 7} |
2 | tote bag | 49 | coach | handbags | 9 | {"author": "olivia", "rating": 8.3} |
3 | sling bag | 75 | coach | purses | 9 | {"author": "alice", "rating": 9.2} |
4 | sneakers shoes | 40 | adidas | shoes | 10 | {"author": "olivia", "rating": 9} |
5 | running shoes | 89 | nike | shoes | 10 | {"author": "olivia", "rating": 8.5} |
Find oneβ
A straightforward query is to read document by applying a filter on a field. As an example, using filter
in above collection to get a product where brand = "adidas"
product, err := catalogs.ReadOne(ctx, filter.Eq("Brand", "adidas"))
if err != nil {
panic(err)
}
fmt.Println(product)
Above query would return a single document matching the filter. To read multiple documents:
it, err := catalogs.Read(ctx, filter.Eq("Brand", "adidas"))
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Println(product)
}
Find manyβ
Or to read all documents from the collection:
it, err := catalogs.ReadAll(ctx)
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Printf("%+v\n", product)
}
By default, string comparison is case-sensitive. To learn, how to make case-insensitive queries, see the case-insensitive queries section.
Filtering on multiple fieldsβ
Single field filtering is useful but what if you need to also filter by price
. Following is an example where we are
reading all the products where brand = "adidas"
and price < 50
it, err := catalogs.Read(ctx,
filter.And(
filter.Eq("Brand", "adidas"),
filter.Lt("Price", 50),
),
)
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Printf("%+v\n", product)
}
Reading specific fieldsβ
Instead of reading all the fields of a document, fields projection allows reading specific fields. As an above example,
let's say you only need to read name
, price
and brand
fields from a document.
it, err := catalogs.Read(ctx,
filter.And(
filter.Eq("Brand", "adidas"),
filter.Lt("Price", 50),
),
fields.Include("Name").
Include("Price").
Include("Brand"),
)
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Printf("%+v\n", product)
}
Users can either specify include
fields or exclude
fields, but not both.
Applying range conditionsβ
Many times the need for filtering is based on range on a numeric field. A range can be applied to any numeric field and
in fact multiple numeric fields can be part of a single filter. Letβs take an example of reading all the products that
are price
less than 50 and have a popularity
score of greater than or equal to 8.
it, err := catalogs.Read(ctx,
filter.And(
filter.Lt("Price", 50),
filter.Gte("Popularity", 8),
),
)
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Printf("%+v\n", product)
}
Querying by metadataβ
Tigris automatically generates following metadata fields for the document:
- created_at: Time when this document was added to database.
- updated_at: Time when this document was last modified. This field is only generated once an existing document is modified.
These generated fields are queryable by user. For example, to fetch documents inserted within a 24 hour period:
it, err := catalogs.Read(ctx, filter.And(
filter.Gte("created_at", "2022-01-01T17:29:28.000Z"),
filter.Lt("created_at", "2022-01-02T17:29:28.000Z"),
),
)
Applying multiple logical filtersβ
Even after applying multiple AND conditions, what if you need something even more complex? What about reading documents
where we need a logical OR
on brand
but also need to apply logical AND
on some other fields. Let's read products where the
brand
is either "adidas" or "coach" but the price
should be less than 50 and the product should be popular
.
it, err := catalogs.Read(ctx, filter.And(
filter.Or(
filter.Eq("Brand", "adidas"),
filter.Eq("Brand", "coach")),
filter.And(
filter.Lt("Price", 50),
filter.Gte("Popularity", 8)),
),
)
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Printf("%+v\n", product)
}
Querying nested fieldsβ
As we can see all the above examples are for top level fields but what if you have an object, and you want to filter
documents based on one of the nested field. Taking the above data, if you want to get all the products which have labels
set as "shoes" but should have rating
greater than 7.
it, err := catalogs.Read(ctx,
filter.And(
filter.Eq("Labels", "shoes"),
filter.Gt("Reviews.Rating", 7),
),
)
if err != nil {
panic(err)
}
var product Catalog
for it.Next(&product) {
fmt.Printf("%+v\n", product)
}
Case-insensitive queriesβ
By default, all String comparisons are case-sensitive. However, if you need to ignore the case
then set the case to ci
in the collation object. The following example demonstrates a case-insensitive
query for brand
"Adidas"
product, err := catalogs.ReadWithOptions(ctx,
filter.Eq("Brand", "Adidas"),
&tigris.ReadOptions {
Collation: & driver.Collation {
Case: "ci",
},
},
)
if err != nil {
panic(err)
}
fmt.Println(product)
Above query will match terms ["adidas", "aDiDas", "Adidas", "adiDas"]
etc.