Docs 菜单
Docs 主页
/ / /
Mongoid
/

集合配置

在此页面上

  • 配置文档collection
  • 时间序列集合
  • 固定大小集合
  • 在集合上设置默认排序规则
  • collection 管理 Rake 任务

您可以使用 store_in宏指定文档的collection选项。该宏接受:collection_options参数,该参数可以包含驱动程序支持的任何collection选项。

注意

要应用这些选项,必须预先显式创建集合。这应使用集合管理 Rake 任务来完成。

有关集合选项的更多信息,请参阅驱动程序集合页面

注意

collection选项取决于驱动程序版本和 MongoDB Server 版本。某些选项(例如time-series collection)可能在较旧的server版本上不可用。

class Measurement
include Mongoid::Document
field :temperature, type: Integer
field :timestamp, type: Time
store_in collection_options: {
time_series: {
timeField: "timestamp",
granularity: "minutes"
},
expire_after: 604800
}
end
class Name
include Mongoid::Document
store_in collection_options: {
capped: true,
size: 1024
}
end
class Name
include Mongoid::Document
store_in collection_options: {
collation: {
locale: 'fr'
}
}
end

如果您为文档指定集合选项,则必须在使用前显式创建相应的集合。为此,请使用提供的db:mongoid:create_collections Rake 任务:

$ rake db:mongoid:create_collections

通过在 Rails 控制台中运行,创建 collection 命令也仅适用于一种模型:

# Create collection for Model
Model.create_collection

后退

验证

来年

索引管理