模块:Mongoid::CollectionConfigurable::ClassMethods

定义于:
lib/mongoid/collection_configurable.rb

实例方法摘要折叠

实例方法详细信息

# create_collection (force: false) ⇒对象

为调用的 Mongoid 模型创建集合。

此方法不会重新创建现有集合。

如果文档包含带有“collection_options”键的“store_in”宏,

these options are used when creating the collection.

参数:

  • force ( true | false ) (默认为: false

    如果为 true,该方法将在创建新集合之前删除现有集合。 如果为 false,该方法将仅创建新集合(数据库中不存在的集合)。

引发:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongoid/collection_configurable.rb', line 24

def create_collection(强制: false)
  if collection_name.空?
    # 这很可能是一个匿名类,我们忽略它们。
    return
  end
  if collection_name.匹配(/ ^system\. /)
    # 我们不对系统集合执行任何操作。
    return
  end
  if force
    集合.删除
  end
  if coll_options = 集合.database.list_collections(过滤器: { 名称: collection_name.to_s }).first
    if force
      提高 Errors::DropCollectionFailure.new(collection_name)
    else
      记录器.debug(
        " MONGOID: Collection ' #{ collection_name } '已经存在" +
        " in 数据库 ' #{ database_name } ' with options ' #{ coll_options } '. "
      )
    end
  else
    开始
      集合.database[collection_name, storage_options.获取(:collection_options, {})].创建
    救援 mongo::错误::OperationFailure => e
      提高 Errors::CreateCollectionFailure.new(
        collection_name,
        storage_options[:collection_options],
        e
      )
    end
  end
end