You are here: Basics Operations & Concepts > Querying > SODA Query > Contains on Collections and Arrays

SODA Special Cases Examples

This topic contains a examples which demonstrate special behavior for some types in SODA. Take also a look at the other SODA examples.

Contains on Collections and Arrays

Collections and arrays have a special behavior in SODA to make them easier to query. For example you can simple use a constrain directly on a collection-field to check if it contains that value.

Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.

Query query = container.query();
query.constrain(BlogPost.class);
query.descend("tags").constrain("db4o");

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

Constrains on Collection Members

When you have a collection or array field, you can simply descend further to the collection-member fields. This allows you query for a object, which has a collection and certain objects in that collection.

Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.

Query query = container.query();
query.constrain(BlogPost.class);
query.descend("authors").descend("name").constrain("Jenny");

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Descend into collection members

Contains Key on Maps

You can check a dictionary if it contains a certain key. Similar to collections, you just can directly use a constrain on the collection field. This will compare the value with the keys of the Map.

Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.

Query query = container.query();
query.constrain(BlogPost.class);
query.descend("metaData").constrain("source");

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Map contains a key constrain

Return the Objects of a Field

With SODA you can navigate to a field and return the objects of that field. Note that this only works for reference objects and not for value objects like strings and numbers.

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

// returns the pilot of these cars
ObjectSet<Object> result = query.descend("pilot").execute();
SodaQueryExamples.java: Return the object of a field

Mixing With Query By Example

When you have a reference type field, you can also use a query by example constrain for that field. Pass a new object as an example for this.

Note that when you pass a persisted object, it will compare it by object identity and not use it as example. You can force this behavior by adding an explicit by example constrain.

Query query = container.query();
query.constrain(Car.class);
// if the given object is not stored,
// it will behave like query by example for the given object
final Pilot examplePilot = new Pilot(null, 42);
query.descend("pilot").constrain(examplePilot);

ObjectSet<Object> carsOfPilot = query.execute();
SodaQueryExamples.java: Mix with query by example

Dynamically Typed

SODA is a dynamically query language. By default SODA acts like a filter on all stored objects. You just add constrains which filters the objects to the desired output.

An example for this behavior: You just add an field-constraint without any type-constrain on the object. This will return all objects which have such a field and match the constrain.

Note that such queries do not utilize any index and therefore show bad performance.

Query query = container.query();
// You can simple filter objects which have a certain field
query.descend("name").constrain(null).not();

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Pure field constrains

This also means that you can query for not existing fields. SODA will not complain if a field doesn't exist. Instead it won't return any object, because no object could satisfy the constrain.

Query query = container.query();
query.constrain(Pilot.class);
// using not existing fields doesn't throw an exception
// but rather exclude all object which don't use this field
query.descend("notExisting").constrain(null).not();

ObjectSet<Object> result = query.execute();
SodaQueryExamples.java: Using not existing fields excludes objects