Collection Configuration
On this page
Configuring a Document Collection
You can specify collection options for documents using the store_in
macro.
This macro accepts :collection_options
argument, which can contain any collection
options that are supported by the driver.
Note
In order to apply the options, the collection must be explicitly created up-front. This should be done using Collection Management Rake Task.
Please refer to the driver collections page for the more information about collection options.
Note
Collection options depend on the driver version and MongoDB server version. It is possible that some options, like time series collections, are not available on older server versions.
Time Series Collection
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
Capped Collections
class Name include Mongoid::Document store_in collection_options: { capped: true, size: 1024 } end
Set a Default Collation on a Collection
class Name include Mongoid::Document store_in collection_options: { collation: { locale: 'fr' } } end
Collection Management Rake Task
If you specify collection options for a document, then the corresponding collection
must be explicitly created prior to use. To do so, use the provided
db:mongoid:create_collections
Rake task:
$ rake db:mongoid:create_collections
The create collections command also works for just one model by running in Rails console:
# Create collection for Model Model.create_collection