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.

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.

  • Filters.eq: equal to is used for exact matching.
  • Filters.lt: less than is used for matching documents using less than criteria.
  • Filters.lte: less than or equal to is similar to Filters.lt but also matches for equality.
  • Filters.gt: greater than is used for matching documents using greater than criteria.
  • Filters.gte: greater than or equal to is similar to Filters.gt but also matches for equality.
  • : not is used for matching documents using not equal criteria.
  • : regex is used for matching documents using a regular expression.
  • : contains is used for matching documents with fields containing the given substring.

For multiple conditions, there are two logical operators supported.

  • Filters.or: Combines multiple filter operators and returns documents when either condition is met.
  • Filters.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.

TigrisCollection<Catalog> catalog = tigrisDatabase.getCollection(Catalog.class);

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}

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"

Optional<Catalog> product = catalog.readOne(Filters.eq("brand", "adidas"));

System.out.println(product);

Above query would return a single document matching the filter. To read multiple documents:

Iterator<Catalog> products = catalog.read(Filters.eq("brand", "adidas"));

while (products.hasNext()) {
Catalog product = products.next();
System.out.println(product);
}

Find many

Or to read all documents from the collection:

Iterator<Catalog> products = catalog.readAll();

while (products.hasNext()) {
Catalog product = products.next();
System.out.println(product);
}
tip

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

Iterator<Catalog> iterator = catalog.read(
Filters.and(
Filters.eq("brand", "adidas"),
Filters.lt("price", 50)
)
);

while (iterator.hasNext()) {
Catalog product = iterator.next();
System.out.println(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.

catalog.read(
Filters.and(
Filters.eq("brand", "adidas"),
Filters.lt("price", 50)
),
ReadFields.newBuilder().includeField("name").includeField("price").includeField("brand").build()
);
caution

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.

catalog.read(
Filters.and(
Filters.lt("price", 50),
Filters.gte("popularity", 8)
)
);

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.read(
Filters.and(
Filters.gte("created_at", "2022-01-01T17:29:28.000Z"),
Filters.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.

catalog.read(
Filters.and(
Filters.or(
Filters.eq("brand", "adidas"),
Filters.eq("brand", "coach")
),
Filters.and(
Filters.lt("price", 50),
Filters.gte("popularity", 8)
)
)
);

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.read(
Filters.and(
Filters.eq("labels", "shoes"),
Filters.gt("reviews.rating", 7)
)
);

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"

// TODO

Above query will match terms ["adidas", "aDiDas", "Adidas", "adiDas"] etc.

Sort

Sort not presently supported in the Java SDK

Sort examples

Sort not presently supported in the Java SDK

Pagination

Pagination not presently supported in the Java SDK

Pagination example

Pagination not presently supported in the Java SDK