Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 mongoid 和 rails 4。我有一个模型,它有一个散列类型的“数据”字段。该字段将存储动态键值对。我知道我们可以为静态字段添加索引,但是否可以为“数据”的键添加索引以及应该如何完成。
如果索引不在动态字段中,您可以按照相同的方式添加,如下所示:
class Person field :data, type: Hash index('data.foo' => 1) end
我很确定 Mongoid 不会验证字段是否存在,因此您可以在模型上创建任何索引。此外,如果您没有包含该字段的所有文档,我还会将该sparse选项添加到索引中,这样您就可以节省一些内存空间。
sparse