Inequality and Range Querying
If you have a field that contains numbers, and you’d like to be able to do more than just search for records that exactly match a given value, then you’re probably looking for the range
index type.
Range indexes allow you to execute queries that find all records with a value less-than, greater-than, or between specified values.
You can also do equality comparisons, so you don’t need both an exact
and range
index over the same field.
Note that range indexes can only be applied to fields whose data type has a defined sort order.
If you want to only find movies less than an hour long, you might use this:
movies.query do |movie|
movie.runningTime.lt(60)
end
Here, #lt
means “less-than”.
Alternately, let’s say you’re only after new movies, once produced in or after 2010:
movies.query do |movie|
movie.year.gte(2010)
end
In this example, #gte
means “greater-than or equal”.
Range indexes support all the inequality operators you’d expect:
lt
(less-than);lte
(less-than-or-equal);gt
(greater-than); andgte
(greater-than-or-equal)
They also support:
eq
(equality)
As well as the “combo” operator, between
, which takes two arguments, min
and max
, as in this example, which will find movies made in 2000, 2001, or 2002:
movies.query do |movie|
movie.year.between(2000, 2002)
end
The values passed to #between
are always inclusive (records which match the value specified will be included in the query result set).