Skip to main content

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.

Filter​

Filter provides the following comparison operators with the same semantics as in virtually all programming languages.

  • SelectorFilterOperator.EQ: equal to is used for exact matching.
  • SelectorFilterOperator.LT: less than is used for matching documents using less than criteria.
  • SelectorFilterOperator.LTE: less than or equal to is similar to SelectorFilterOperator.LT but also matches for equality.
  • SelectorFilterOperator.GT: greater than is used for matching documents using greater than criteria.
  • SelectorFilterOperator.GTE: greater than or equal to is similar to SelectorFilterOperator.GT but also matches for equality.

For multiple conditions, there are two logical operators supported.

  • LogicalOperator.OR: Combines multiple filter operators and returns documents when either condition is met.
  • LogicalOperator.AND: Combines multiple filter operators and returns documents when all the conditions are met.

Example Collection​

The first step is to create the collection object.

const catalog: Collection<Catalog> = db.getCollection<Catalog>("catalog");

Assuming an e-commerce website that has the above collection catalog and has 5 products(documents) in it.

idnamepricebrandlabelspopularityreviews
1fiona handbag99.9michael korspurses8{"author": "alice", "rating": 7}
2tote bag49coachhandbags9{"author": "olivia", "rating": 8.3}
3sling bag75coachpurses9{"author": "alice", "rating": 9.2}
4sneakers shoes40adidasshoes10{"author": "olivia", "rating": 9}
5running shoes89nikeshoes10{"author": "olivia", "rating": 8.5}

Simple read query​

A straightforward query is to read documents by applying a filter on a field. As an example, applying the filter on the above collection by reading only the products that are of brand "adidas".

catalog.findOne({
op: SelectorFilterOperator.EQ,
fields: {
brand: "adidas"
}
}).then(value => {
const product: Catalog = <Catalog> value;
console.log(product.name); // 'sneakers shoes'
console.log(product.price); // 40
});
note

String comparison is case sensitive.

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 is "adidas" and price is less than 50.

catalog.findMany({
op: LogicalOperator.AND,
selectorFilters: [
{
brand: "adidas"
},
{
op: SelectorFilterOperator.LT,
fields: {
price: 50
}
}
]
}).then(value => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});

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.

catalog.findMany({
op: LogicalOperator.AND,
selectorFilters: [
{
brand: "adidas"
},
{
op: SelectorFilterOperator.LT,
fields: {
price: 50
}
}
]
}, {
include: ["name", "price", "brand"],
}).then(value => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});
note

To exclude use excludeField("fieldName").

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.

catalog.findMany({
op: LogicalOperator.AND,
selectorFilters: [
{
op: SelectorFilterOperator.LT,
fields: {
price: 50
}
},
{
op: SelectorFilterOperator.GTE,
fields: {
popularity: 8
}
}
]
}).then(value => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});

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:

catalog.findMany({
op: LogicalOperator.AND,
selectorFilters: [
{
op: SelectorFilterOperator.GTE,
fields: {
created_at: "2022-01-01T17:29:28.000Z"
}
},
{
op: SelectorFilterOperator.LT,
fields: {
created_at: "2022-01-02T17:29:28.000Z"
}
}
]
}).then(value => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});

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.

const logicalFilter: LogicalFilter <ProductCatalog> = {
op: LogicalOperator.OR,
selectorFilters: [
{
op: SelectorFilterOperator.LT,
fields: {
price: 50
}
},
{
op: SelectorFilterOperator.GTE,
fields: {
popularity: 8
}
}
],
logicalFilters: [{
op: LogicalOperator.AND,
selectorFilters: [
{
op: SelectorFilterOperator.LT,
fields: {
price: 50
}
},
{
op: SelectorFilterOperator.GTE,
fields: {
popularity: 8
}
}
]
}],
};

catalog.findMany(logicalFilter).then(value => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});

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.

catalog.findMany({
op: LogicalOperator.AND,
selectorFilters: [
{
labels: "shoes"
},
{
op: SelectorFilterOperator.GT,
fields: {
reviews: {
rating: 7
}
}
}
]
}).then(value => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});

Reading all the documents in a collection​

You can also read all documents from your collection by calling readAll API.

catalog.findAllStream({
onEnd() {
// handle onEnd
},
onNext(product: Product) {
console.log(product);
},
onError(_error: Error) {
console.log(_error);
},
});

Case-Insensitive Queries​

Tigris supports Collation which allows you to specify string comparison rules. Default is case-sensitive filtering on text fields. But if you need to ignore the case then set the case to ci in the collation object. The following example is showing when you need to query for a brand "adidas", but you don't care about the case.

const options = new ReadRequestOptions();
options.collation = {
case: "ci",
};
catalog.findMany(
{
brand: "adidas",
},
null,
null,
options
)
.then((value) => {
const products: Catalog[] = <Catalog[]>value;
for (const i in products) {
console.log(products[i]);
}
});