【问题标题】:Mongoid/mongodb indexingMongoid/mongodb 索引
【发布时间】:2023-03-26 08:02:01
【问题描述】:

我正在使用 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 是 Hash,store 是
{:brokers => [{:person_id => 2134, :name => 'Broker 1'}],
:office =>> [{:person_id => 2131, :name => 'Office 1'}, {:person_id => 1231, :name => 'Office 2'}]}

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

【问题讨论】:

  • 不知道为什么人们只是投反对票而不评论什么是错的...... :(
  • 什么是“office = 465456”?那是person_id吗?很难准确说出你的意图是什么,我猜这可能会导致投票失败。
  • 最近,似乎有一个真正的横冲直撞(没有评论)带有 Mongoid 标签的完全合理的问题。

标签: mongodb mongoid


【解决方案1】:

如果将其存储为散列,则需要多个索引。因为您必须为散列中的每个键索引办公室名称。如果将其存储为数组,则只需要 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

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 2012-08-24
      • 2011-12-03
      • 2021-01-06
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多