I am implementing a ruby on rails app with mongodb/mongoid and I am bit confused about the better indexing/searching structure.
I have a field staff in a model and a staff can be either of type – production, broker, office.
Each staff is a Person. Each type can have multiple staff.
So I have two approaches:
1). Make staff as an array and store it like
[{: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 is a Hash and store is as
{:brokers => [{:person_id =>
2134, :name => 'Broker 1'}],
:office =>> [{:person_id =>
2131, :name => 'Office 1'}, {:person_id => 1231, :name => 'Office
2'}]}
I want to index these documents and should be able to search documents like, where office = ‘465456’.
If you store it as a hash you need several indexes. Since you then have to index the office name for each of the keys in the hash. If you store it as an array you only need 1 index.