模块:Mongoid::Indexable::ClassMethods

定义于:
lib/mongoid/indexable.rb

实例方法摘要折叠

实例方法详细信息

# add_indexestrue

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

例子:

添加 Mongoid 内部索引。

Person.add_indexes

返回:

  • ( true )

    如果操作成功。



76
77
78
79
80
81
# File 'lib/mongoid/indexable.rb', line 76

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

# create_indexestrue

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

例子:

为类创建索引。

Person.create_indexes

返回:

  • ( true )

    如果操作成功。



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

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

    索引选项。

返回:

  • (哈希)

    索引选项。



96
97
98
99
100
101
# File 'lib/mongoid/indexable.rb', line 96

def 索引(index)(spec, 选项 = nil)
  规范 = 规范.new(self, spec, 选项)
  if !index_specifications.包括?(规范)
    index_specifications.推动(规范)
  end
end

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

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

例子:

获取索引规范。

Model.index_specification(name: 1)

参数:

  • index_hash (哈希)

    索引键/方向对。

  • index_name string (默认为: nil

    索引名称。

返回:

  • 规范

    找到的规范。



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

def index_说明(index_hash, index_name = nil)
  索引(index) = OpenStruct.new(字段: index_hash.密钥, 键: index_hash)
  index_specifications.检测 do |spec|
    spec == 索引(index) || (index_name && index_name == spec.名称)
  end
end

# remove_indexestrue

将实际的索引删除注释发送到MongoDB驾驶员,但保持_id不变。

例子:

删除该类的索引。

Person.remove_indexes

返回:

  • ( true )

    如果操作成功。



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mongoid/indexable.rb', line 51

def remove_indexes
  indexed_database_names. do |database|
    通过(database: database) do |klass|
      开始
        klass.集合.索引(会话: _session). do |spec|
          除非 spec[" name "] == " _id_ "
            klass.集合.索引(会话: _session).drop_one(spec[" key "])
            记录器.信息(
              " MONGOID: Removed 索引 ' #{ spec [ " name " ] } ' on 集合 " +
              " ' #{ klass . 集合 . name } ' in 数据库 ' #{ 数据库 } '. "
            )
          end
        end
      救援 mongo::错误::OperationFailure end
    end
  end  true
end