模块:Mongoid::Indexable::ClassMethods

定义于:
lib/mongoid/indexable.rb

实例方法摘要折叠

实例方法详细信息

# add_indexestrue

将默认索引添加到根文档(如果尚不存在)。 目前这只是 _type。

例子:

添加 Mongoid 内部索引。

Person.add_indexes

返回:

  • ( true )

    如果操作成功。



72
73
74
75
76
77
# File 'lib/mongoid/indexable.rb', line 72

def add_indexes
  if 会遗传吗? && !index_keys.包括?(discriminator_key.to_sym => 1)
    索引(index)({ discriminator_key.to_sym => 1 }, unique: false, 背景: true)
  end
  true
end

# create_indexestrue

将实际的索引创建注释发送到 MongoDB 驱动程序

例子:

为类创建索引。

Person.create_indexes

返回:

  • ( true )

    如果操作成功。



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mongoid/indexable.rb', line 24

def create_indexes
  return 除非 index_specifications

  default_options = { 背景: 配置.background_indexing }

  index_specifications. do |spec|
    key, 选项 = spec.key, default_options.合并(merge)(spec.选项)
    if database = 选项[:database]
      通过(database: database) do |klass|
        klass.集合.索引(会话: _session).create_one(key, 选项.除了(:database))
      end
    else
      集合.索引(会话: _session).create_one(key, 选项)
    end
  end  true
end

#index(spec, options = nil) ⇒ Hash

为提供的单键或复合键添加索引定义。

例子:

创建基本索引。

class Person
  include Mongoid::Document
  field :name, type: String
  index({ name: 1 }, { background: true })
end

参数:

  • spec (哈希)

    索引规范。

  • 选项 哈希 (默认为: nil

    索引选项。

返回:

  • (哈希)

    索引选项。



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid/indexable.rb', line 92

def 索引(index)(spec, 选项 = nil)
  规范 = 规范.new(self, spec, 选项)

  # Indexable::Specification 实例的相等性测试不会
  # 考虑任何选项,这意味着不会比较名称。这意味着
  # 一个索引具有与另一个索引不同的选项,并且不同
  # 名称,除非有重复的索引声明,否则将被静默忽略
  # 是允许的。
  return 除非 Mongoid.allow_duplicate_index_declarations || !index_specifications.包括?(规范)

  index_specifications.推动(规范)
end

# index_specation (index_hash, index_name = nil) ⇒规范

获取所提供键的索引规范。

例子:

获取索引规范。

Model.index_specification(name: 1)

参数:

  • index_hash (哈希)

    索引键/方向对。

  • index_name string (默认为: nil

    索引名称。

返回:

  • 规范

    找到的规范。



114
115
116
117
118
# File 'lib/mongoid/indexable.rb', line 114

def index_说明(index_hash, index_name = nil)
  index_specifications.检测 do |spec|
    spec.superficial_match?(键: index_hash, 名称: index_name)
  end
end

# remove_indexestrue

将实际的索引删除注释发送到MongoDB驱动程序,但保持_id不变。

例子:

删除该类的索引。

Person.remove_indexes

返回:

  • ( true )

    如果操作成功。



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mongoid/indexable.rb', line 48

def remove_indexes
  indexed_database_names. do |database|
    通过(database: database) do |klass|
      klass.集合.索引(会话: _session). do |spec|
        来年 if spec[' name '] == ' _id_ '

        klass.集合.索引(会话: _session).drop_one(spec['key'])
        记录器.信息(
          " MONGOID: Removed 索引 ' #{ spec [' name' ] } ' on 集合 " +
          " ' #{ klass . 集合 . name } ' in 数据库 ' #{ 数据库 } '. "
        )
      end
    救援 mongo::错误::OperationFailure
    end
  end  true
end