You are here: Basics Operations & Concepts > Querying > SODA Query > Type Constraint

SODA Query Examples

Here's a collection of SODA-query examples. These queries assume that there's a Pilot class with a name and age, a Car class with a pilot and name and a BlogPost class with list of tags, authors and a Map of meta-data.

There are also a few examples for special cases.

Type Constraint

This is the most basic and most used constraint for SODA-queries. SODA acts like a filter on all stored objects. But usually you're only interested for instances of a certain type. Therefore you need to constrain the type of the result.

final Query query = container.query();
query.constrain(Pilot.class);

ObjectSet result = query.execute();
SodaQueryExamples.java: Type constrain for the objects

Field Constraint

You can add constrains on fields. This is done by descending into a field and constrain the value of that field. By default the constrain is an equality comparison.

final Query query = container.query();
query.constrain(Pilot.class);
query.descend("name").constrain("John");

final ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: A simple constrain on a field

Comparisons

You can do comparison on the field-values. For example to check if something is greater or smaller than something else.

Query query = container.query();
query.constrain(Pilot.class);
query.descend("age").constrain(42).greater();

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: A greater than constrain
Query query = container.query();
query.constrain(Pilot.class);
query.descend("age").constrain(42).greater().equal();

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: A greater than or equals constrain

Combination of Constraints (AND, OR)

You can combine different constraints with an 'AND' or 'OR' condition. By default all constrains are combined with the 'AND' condition.

Query query = container.query();
query.constrain(Pilot.class);
query.descend("age").constrain(42).greater()
        .or(query.descend("age").constrain(30).smaller());

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Logical combination of constrains

Not-Constrain

You can invert a constrain by calling the not-method.

Query query = container.query();
query.constrain(Pilot.class);
query.descend("age").constrain(42).not();

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Not constrain

String Comparison

There are special compare operations for strings. By default strings are compared by equality and the comparison is case sensitive.

There's the contains-comparison which checks if a field contains a substring. The like-comparison is the case-insensitive version of the contains-comparison.

Also a start-with- and a ends-with-caparison is available for strings. For this you can specify if the comparison is case sensitive or not.

Note that string comparison do not utilize any index in db4o and therefore show bad performance.

Query query = container.query();
query.constrain(Pilot.class);
// First strings, you can use the contains operator
query.descend("name").constrain("oh").contains()
        // Or like, which is like .contains(), but case insensitive
        .or(query.descend("name").constrain("AnN").like())
                // The .endsWith and .startWith constrains are also there,
                // the true for case-sensitive, false for case-insensitive
        .or(query.descend("name").constrain("NY").endsWith(false));

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: String comparison

Compare Field With Existing Object

When you have a reference type field, you can compare this field with a certain object. It will compare the field and the object by object identity.

Note that this comparison only works with stored objects. When you use a not yet stored object as constrain, it will use query by example. To force a comparison by object identity, you can add a .Identiy() call.

Pilot pilot = container.query(Pilot.class).get(0);

Query query = container.query();
query.constrain(Car.class);
// if the given object is stored, its compared by identity
query.descend("pilot").constrain(pilot);

ObjectSet<Object> carsOfPilot = query.execute();
SodaQueryExamples.java: Compare with existing object

Descend Deeper Into Objects

You can descend deeper into the objects by following fields. This allows you to setup complex constraints on nested objects. Note that the deeper you descend into the objects, the more expensive the query is to execute.

Query query = container.query();
query.constrain(Car.class);
query.descend("pilot").descend("name").constrain("John");

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Descend over multiple fields