Skip to content

Commit 41cb4b7

Browse files
MONGOID-5370 Add collection options support (#5452)
Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com>
1 parent 44c26f1 commit 41cb4b7

File tree

1 file changed

+69
-24
lines changed

1 file changed

+69
-24
lines changed

source/reference/collection-configuration.txt

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,96 @@ Collection Configuration
1010
:depth: 2
1111
:class: singlecol
1212

13+
Configuring a Document Collection
14+
=================================
1315

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>`.
1624

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.
2028

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+
----------------------
2237

2338
.. code-block:: ruby
2439

25-
class Name
40+
class Measurement
2641
include Mongoid::Document
27-
end
28-
client = Name.collection.client
2942

30-
Then create the collection:
43+
field :temperature, type: Integer
44+
field :timestamp, type: Time
3145

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
3354

34-
client["names", :capped => true, :size => 1024].create
3555

36-
To create a capped collection from the Mongo console:
3756

38-
.. code-block:: javascript
57+
Capped Collections
58+
------------------
3959

40-
db.createCollection("name", { capped: true, size: 1024 });
60+
.. code-block:: ruby
4161

62+
class Name
63+
include Mongoid::Document
64+
65+
store_in collection_options: {
66+
capped: true,
67+
size: 1024
68+
}
69+
end
4270

4371
Set a Default Collation on a Collection
4472
---------------------------------------
4573

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
4975

50-
To create a collection with a default collation with the driver:
76+
class Name
77+
include Mongoid::Document
5178

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+
===============================
5390

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:
5594

56-
To create a collection with a default collation from the Mongo console:
95+
.. code-block:: bash
5796

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
59103

60-
db.createCollection("name", { collation: { locale: 'fr' } });
104+
# Create collection for Model
105+
Model.create_collection

0 commit comments

Comments
 (0)