2

Trying to work out the best way to add an index on a polymorphic association in mongoid

I have

class User
  include Mongoid::Document

class Student < User
  include Mongoid::Document

class Parent < User
  include Mongoid::Document

when mongoid creates data it creates a _type field

Parent
{
"_id" : ObjectId("51f06367b5b60561d0000003"),
"_type" : "Parent"
}

I want to search on type. e.g. Student.all and noticed with the explain() there is no implict index created by mongoid.

To solve this I added an index in the User class

index({ _type: 1 })

Wondering is there a way to get mongoid to create index's automatically? Or is there a better way to do this apart from adding the index on the User model?

4

1 回答 1

1

您在这里拥有的是subclass,而不是 Mongoid 文档中提到的多态性。但是,没有自动化的方法。手动添加索引是推荐的方法,所以你已经准备好了!

http://mongoid.org/en/mongoid/v3/documents.html#inheritance

于 2014-10-07T07:10:30.023 回答