GTDev
GTDev

Reputation: 5528

Mongoid caseinsensitive query

I want to make a case insensitive mongoid query on a mission title.

Lets say the model looks like:

class Mission
  include Mongoid::Document

  field :title
  filed :description
end

Can this be accomplished with a mongoid scope/index and what would it look like?

Also, Is better practice to denormalize the data and hold a indexable lowercase field and if so what would that look like?

Thank you very much.

Upvotes: 1

Views: 1648

Answers (1)

Adam B
Adam B

Reputation: 1774

If this is a field you will be indexing the best solution is to add an additional field which holds an lower/upper case version of your field as suggested here.

If not, you can query by a case insensitive regex. This will have performance impacts however so tred lightly.

db.Mission.find({"title" : /foo/i})

Upvotes: 2

Related Questions