2012-05-15 179 views
0

我正在用mongodb/mongoid在rails应用上实现一个ruby,我对有更好的索引/搜索结构感到困惑。 我有一个模型领域staff和工作人员可以是任何类型 - 生产,经纪人,办公室。 每个工作人员是Person。每种类型可以有多个员工。Mongoid/mongodb索引

所以我有两种方法:

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)。让是一个Hash和存储是
{:brokers => [{:person_id => 2134, :name => 'Broker 1'}],
:office =>> [{:person_id => 2131, :name => 'Office 1'}, {:person_id => 1231, :name => 'Office 2'}]}

我想要索引这些文件,应该能够搜索等,其中办公=“465456”的文件。

+0

不知道为什么人们只是下降投票没有评论有关什么是错... :( – Pravin

+0

什么是“办公室= 465456“?这是person_id吗?很难确切地说出你的意图是什么,我猜这可能会导致反对票。 –

+0

最近,似乎有一个真正的downvoting(没有评论)的横冲直撞具有Mongoid标签的合理问题。 – theTRON

回答

3

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

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