Query()with conditions as arguments:
HasPrefixon a device location. Where does this come from? ObjectBox generates a
Device_struct for you to reference available properties conveniently. This also allows code completion in your IDE and avoids typos: correctness is checked at compile time (string based queries would only be checked at run-time).
Device_in the same package:
Device_to construct type-specific conditions in place and combining them, forming the full query. The following example looks for devices located in the U. S. with profile number 42.
Queryyou should cache the
Queryobject and re-use it. To make a
Querymore reusable you can change the values, or query parameters, of each condition you added even after the
Queryis built. Let's see how.
FirstNamevalues. First, we build a regular
FirstName. Because we have to pass an initial parameter value to
equal()but plan to override it before running the
Querylater, we just pass an empty string:
Query. To set a value for the
FirstNameparameter we call
Queryand pass the
FirstNameproperty and the new parameter value:
Alias()right after specifying the condition:
.Limit()methods to help you do that
Offset(n uint64):the first
nresults are skipped.
Limit(n uint64):at most
nresults of this query are returned.
LessThan()there are also conditions like:
Between()to filter for values that are between the given two (inclusive)
NotIn()to filter for values that match any in the given set,
Contains()for extended String filtering.
Find()returns a slice of the matching objects,
FindIds()fetches just the IDs of the matching objects as a slice, which can be more efficient in case you don't need the whole object,
Remove()deletes all the matching objects from the database (in a single transaction),
Count()gives you the number of the objects that match the query,
Offset()let you select just part of the result (e. g. for paging)
DescribeParams()is a utility function which returns a human-readable representation of the query.
Personthat can be associated with multiple
Personwith a certain name that also lives on a specific street, we need to query the associated
Addressentities of a
Person. To do this, use the
Person_.Address.Link(cs ...Conditions)method of the generated
Person_variable to tell that the
addressesrelation should be queried and what conditions should be used to filter the addresses:
Person? No problem, links are smart enough to know there's also an implicit relation in the opposite direction. Note the different
boxwe're using here:
query.Property(Property). For example, instead of getting all Users, to just get their email addresses:
FindStrings()(and similar for other types) is a value to be used if the given field is
nilin the database. By default, i.e. when you pass `nil` as the argument, these values are not returned. However, you can specify a replacement value to return if a property is null:
MinDouble(): Finds the minimum value for the given property over all objects matching the query.
MaxFloat64(): Finds the maximum value.
SumFloat64(): Calculates the sum of all values. Note: the integer version detects overflows and returns an error in that case.
Average(): Calculates the average (always a
float64) of all values.
Count(): returns the number of results. This is faster than finding and getting the length of the result array. Can be combined with
Distinct()to count only the number of distinct values.