0

我正在使用 mongodb/mongoid 实现一个 ruby​​ on rails 应用程序,我对更好的索引/搜索结构有点困惑。我staff在模型中有一个字段,工作人员可以是任何一种类型 - 生产、经纪人、办公室。每个员工都是一个Person。每种类型可以有多个员工。

所以我有两种方法:

1)。制作staff as an array和储存它

[{:key => 'broker', :name => "Broker Name", :person_id => "654978"},
{:key => 'office', :name => "Office Staff 1", :person_id => "564654"},
{:key => 'office', :name => 'another office', :person_id => '79878'}]

2)。Make 是 aHash并且 store 是 as
{:brokers => [{:person_id => 2134, :name => 'Broker 1'}],
:office =>> [{:person_id => 2131, :name => 'Office 1'}, {:person_id => 1231, :name => 'Office 2'}]}

我想为这些文档编制索引,并且应该能够搜索诸如 office = '465456' 之类的文档。

4

2 回答 2

3

如果将其存储为哈希,则需要多个索引。因为您必须为散列中的每个键索引办公室名称。如果将其存储为数组,则只需要 1 个索引。

于 2012-05-15T09:31:07.603 回答
2

看起来您真的想存储多个索引;@bjartek 是对的,您需要将这些存储为数组:

class Office
  include Mongoid::Document
  embeds_many :people, as: :staff

  # unsure for polymorphic embeds; perhaps this needs 'staff.name'
  index "people.name"
  index "people.person_id"
  index "people.key"
end

http://mongoid.org/docs/indexing.html

于 2012-05-15T18:30:04.367 回答