Mongoid: query a range of ASCII characters
-
15-04-2021 - |
Question
I have a collection with a string field called named
in my Mongoid based class. I'd like to be able to query for all documents that begin with the letters in the a through f in the name
field, case insensitive. What's the best way to do this through Mongoid? I'm assuming the interface is similar to ActiveRecord, so if it can be done in ActiveRecord, it can probably be done through Mongoid as well.
Solution
This will do the trick
Yourcollection.where(:name => /^[a-f]/i )
i - for case insensitive
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow