This topic contains a examples which demonstrate special behavior for some types in SODA. Take also a look at the other SODA examples.
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.
IQuery query = container.Query(); query.Constrain(typeof (BlogPost)); query.Descend("tags").Constrain("db4o"); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(BlogPost)) query.Descend("tags").Constrain("db4o") Dim result As IObjectSet = query.Execute()
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.
IQuery query = container.Query(); query.Constrain(typeof(BlogPost)); query.Descend("authors").Descend("name").Constrain("Jenny"); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(BlogPost)) query.Descend("authors").Descend("name").Constrain("Jenny") Dim result As IObjectSet = query.Execute()
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 Dictionary.
Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.
IQuery query = container.Query(); query.Constrain(typeof (BlogPost)); query.Descend("metaData").Constrain("source"); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(BlogPost)) query.Descend("metaData").Constrain("source") Dim result As IObjectSet = query.Execute()
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.
IQuery query = container.Query(); query.Constrain(typeof(Car)); query.Descend("name").Constrain("Mercedes"); // returns the pilot of these cars IObjectSet result = query.Descend("pilot").Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Car)) query.Descend("name").Constrain("Mercedes") ' returns the pilot of these cars Dim result As IObjectSet = query.Descend("pilot").Execute()
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.
IQuery query = container.Query(); query.Constrain(typeof(Car)); // if the given object is not stored, // it will behave like query by example for the given object Pilot examplePilot = new Pilot(null, 42); query.Descend("pilot").Constrain(examplePilot); IObjectSet carsOfPilot = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Car)) ' if the given object is not stored, ' it will behave like query by example for the given object Dim examplePilot As New Pilot(Nothing, 42) query.Descend("pilot").Constrain(examplePilot) Dim carsOfPilot As IObjectSet = query.Execute()
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.
IQuery query = container.Query(); // You can simple filter objects which have a certain field query.Descend("name").Constrain(null).Not(); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() ' You can simple filter objects which have a certain field query.Descend("name").Constrain(Nothing).[Not]() Dim result As IObjectSet = query.Execute()
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.
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); // 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(); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) ' using not existing fields doesn't throw an exception ' but rather exclude all object which don't use this field query.Descend("notExisting").Constrain(Nothing).[Not]() Dim result As IObjectSet = query.Execute()