集合配置
配置文档collection
您可以使用 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
collection 管理 Rake 任务
如果您为文档指定集合选项,则必须在使用前显式创建相应的集合。为此,请使用提供的db:mongoid:create_collections
Rake 任务:
$ rake db:mongoid:create_collections
通过在 Rails 控制台中运行,创建 collection 命令也仅适用于一种模型:
# Create collection for Model Model.create_collection