@@ -10,51 +10,96 @@ Collection Configuration
10
10
:depth: 2
11
11
:class: singlecol
12
12
13
+ Configuring a Document Collection
14
+ =================================
13
15
14
- Capped Collections
15
- ------------------
16
+ You can specify collection options for documents using the ``store_in`` macro.
17
+ This macro accepts ``:collection_options`` argument, which can contain any collection
18
+ options that are supported by the driver.
19
+
20
+ .. note::
21
+
22
+ In order to apply the options, the collection must be explicitly created up-front.
23
+ This should be done using :ref:`Collection Management Rake Task<collection-management-task>`.
16
24
17
- Mongoid does not provide a mechanism for creating capped collections on the fly - you
18
- will need to create these yourself one time up front either with the driver or via the
19
- Mongo console .
25
+ Please refer to `the driver collections page
26
+ <https://mongodb.com/docs/ruby- driver/current/reference/collection-tasks/>`_
27
+ for the more information about collection options .
20
28
21
- To create a capped collection with the driver, first retrieve the client:
29
+ .. note::
30
+
31
+ Collection options depend on the driver version and MongoDB server version.
32
+ It is possible that some options, like time series collections, are not available
33
+ on older server versions.
34
+
35
+ Time Series Collection
36
+ ----------------------
22
37
23
38
.. code-block:: ruby
24
39
25
- class Name
40
+ class Measurement
26
41
include Mongoid::Document
27
- end
28
- client = Name.collection.client
29
42
30
- Then create the collection:
43
+ field :temperature, type: Integer
44
+ field :timestamp, type: Time
31
45
32
- .. code-block:: ruby
46
+ store_in collection_options: {
47
+ time_series: {
48
+ timeField: "timestamp",
49
+ granularity: "minutes"
50
+ },
51
+ expire_after: 604800
52
+ }
53
+ end
33
54
34
- client["names", :capped => true, :size => 1024].create
35
55
36
- To create a capped collection from the Mongo console:
37
56
38
- .. code-block:: javascript
57
+ Capped Collections
58
+ ------------------
39
59
40
- db.createCollection("name", { capped: true, size: 1024 });
60
+ .. code-block:: ruby
41
61
62
+ class Name
63
+ include Mongoid::Document
64
+
65
+ store_in collection_options: {
66
+ capped: true,
67
+ size: 1024
68
+ }
69
+ end
42
70
43
71
Set a Default Collation on a Collection
44
72
---------------------------------------
45
73
46
- Mongoid does not provide a mechanism for creating a collection with a default collation.
47
- Like capped collections, you will need to create the collection yourself one time, up-front,
48
- either with the driver or via the Mongo console.
74
+ .. code-block:: ruby
49
75
50
- To create a collection with a default collation with the driver:
76
+ class Name
77
+ include Mongoid::Document
51
78
52
- .. code-block:: ruby
79
+ store_in collection_options: {
80
+ collation: {
81
+ locale: 'fr'
82
+ }
83
+ }
84
+ end
85
+
86
+ .. _collection-management-task:
87
+
88
+ Collection Management Rake Task
89
+ ===============================
53
90
54
- client["name", :collation => { :locale => 'fr'}].create
91
+ If you specify collection options for a document, then the corresponding collection
92
+ must be explicitly created prior to use. To do so, use the provided
93
+ ``db:mongoid:create_collections`` Rake task:
55
94
56
- To create a collection with a default collation from the Mongo console:
95
+ .. code-block:: bash
57
96
58
- .. code-block:: javascript
97
+ $ rake db:mongoid:create_collections
98
+
99
+ The create collections command also works for just one model by running
100
+ in Rails console:
101
+
102
+ .. code-block:: ruby
59
103
60
- db.createCollection("name", { collation: { locale: 'fr' } });
104
+ # Create collection for Model
105
+ Model.create_collection
0 commit comments