Julien
Julien

Reputation: 2319

Mongoid indexes and validations

I have an Rails 5 / Mongoid 7 app that parses files and adds the content to the DB. the parsing is taking more and more time after each file is processed and I think this is because I have a validates_uniqueness_of on one of the fields, as the collection grows larger, that validation needs to check a larger collection, makes sense.

So I figured creating an index on that field would help that, but I was wondering if I should still leave the validates_uniqueness_of there anyway or should I remove it?

Can't really seem to find information about that anywhere.

Model:

class SomeModel
  include Mongoid::Document
  include Mongoid::Timestamps

  field :some_field, type: String

  index({ some_field: 1 }, { unique: true, name: "some_field_index" })

  validates_uniqueness_of :some_field, { case_sensitive: false }
end

Note: I've run rake db:mongoid:create_indexes but I haven't tried a new parse yet, wanted to know how to handle this first.

Upvotes: 0

Views: 252

Answers (1)

Julien
Julien

Reputation: 2319

So I've run several tests and adding the index made a huge difference in processing time, i'll leave my answer here for posterity.

The validates_uniqueness_of can be removed, although now the uniqueness of the field is handled by the index so instead of getting a validation error when trying to save the document, you get an exception thrown, so i had to change how some of the code that was handling the document creation, keep that in mind if you have to deal with a similar situation.

Upvotes: 1

Related Questions