1
1
.. _manual-capped-collection:
2
+ .. _capped_collections_remove_documents:
2
3
3
4
==================
4
5
Capped Collections
@@ -12,66 +13,81 @@ Capped Collections
12
13
:depth: 2
13
14
:class: singlecol
14
15
15
- Overview
16
- --------
16
+ .. facet::
17
+ :name: genre
18
+ :values: reference
17
19
18
- :term:`Capped collections <capped collection>` are fixed-size
19
- collections that support high-throughput operations that insert
20
- and retrieve documents based on insertion order. Capped
21
- collections work in a way similar to circular buffers: once a
22
- collection fills its allocated space, it makes room for new documents
23
- by overwriting the oldest documents in the collection.
20
+ Capped collections are fixed-size collections that insert and retrieve
21
+ documents based on insertion order. Capped collections work similarly to
22
+ circular buffers: once a collection fills its allocated space, it makes
23
+ room for new documents by overwriting the oldest documents in the
24
+ collection.
24
25
25
- See :method:`~db.createCollection()` or :dbcommand:`create`
26
- for more information on creating capped collections.
26
+ Restrictions
27
+ ------------
27
28
28
- .. tip::
29
+ - Capped collections cannot be sharded.
29
30
30
- As an alternative to capped collections, consider MongoDB's
31
- :ref:`TTL (Time To Live) indexes <index-feature-ttl>`. As
32
- described in :ref:`ttl-collections`, these indexes allow you
33
- to expire and remove data from normal collections based on the value
34
- of a date-typed field and a TTL value for the index.
31
+ - You cannot create capped collections on :atlas:`serverless instances
32
+ </tutorial/create-serverless-instance/>`.
35
33
36
- TTL indexes are not compatible with capped collections.
34
+ - Capped collections are not supported in :ref:`Stable API <stable-api>`
35
+ V1.
37
36
37
+ - You cannot write to capped collections in :ref:`transactions
38
+ <transactions>`.
38
39
39
- Behavior
40
- --------
40
+ - The :pipeline:`$out` aggregation pipeline stage cannot write results
41
+ to a capped collection.
41
42
42
- Insertion Order
43
- ~~~~~~~~~~~~~~~
43
+ - You cannot use read concern :readconcern:`"snapshot"` when reading
44
+ from a capped collection.
44
45
45
- Capped collections guarantee preservation of the insertion order. As a
46
- result, queries do not need an index to return documents in insertion
47
- order. Without this indexing overhead, capped collections can support
48
- higher insertion throughput.
46
+ Command Syntax
47
+ --------------
49
48
50
- .. _capped_collections_remove_documents:
49
+ The following example creates a capped collection called ``log`` with a
50
+ maximum size of 100,000 bytes.
51
+
52
+ .. code-block:: javascript
53
+
54
+ db.createCollection( "log", { capped: true, size: 100000 } )
55
+
56
+ For more information on creating capped collections, see
57
+ :method:`~db.createCollection()` or :dbcommand:`create`.
58
+
59
+ Use Cases
60
+ ---------
61
+
62
+ .. include:: /includes/capped-collections/use-ttl-index.rst
63
+
64
+ The most common use case for a capped collection is to store log
65
+ information. When the capped collection reaches its maximum size, old
66
+ log entries are automatically overwritten with new entries.
67
+
68
+ Get Started
69
+ -----------
51
70
52
- Automatic Removal of Oldest Documents
53
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
+ To create and query capped collections, see these pages:
54
72
55
- To make room for new documents, capped collections automatically remove
56
- the oldest documents in the collection without requiring scripts or
57
- explicit remove operations.
73
+ - :ref:`capped-collections-create`
58
74
59
- Consider the following potential use cases for capped
60
- collections:
75
+ - :ref:`capped-collections-query`
61
76
62
- - Store log information generated by high-volume systems. Inserting
63
- documents in a capped collection without an index is close to the
64
- speed of writing log information directly to a file
65
- system. Furthermore, the built-in *first-in-first-out* property
66
- maintains the order of events, while managing storage use.
67
- For example, the :ref:`oplog <capped-collections-oplog>`
68
- uses a capped collection.
77
+ - :ref:`capped-collections-check`
69
78
70
- - Cache small amounts of data in a capped collections. Since caches
71
- are read rather than write heavy, you would either need to ensure
72
- that this collection *always* remains in the working set (i.e. in
73
- RAM) *or* accept some write penalty for the required index or
74
- indexes.
79
+ - :ref:`capped-collections-convert`
80
+
81
+ - :ref:`capped-collections-change-size`
82
+
83
+ - :ref:`capped-collections-change-max-docs`
84
+
85
+ .. _capped-collections-recommendations-and-restrictions:
86
+
87
+ Details
88
+ -------
89
+
90
+ Consider these behavioral details for capped collections.
75
91
76
92
.. _capped-collections-oplog:
77
93
@@ -81,203 +97,65 @@ Oplog Collection
81
97
The :term:`oplog.rs <oplog>` collection that stores a log
82
98
of the operations in a :term:`replica set` uses a capped collection.
83
99
84
- Starting in MongoDB 4.0, unlike other capped collections, the oplog can
85
- grow past its configured size limit to avoid deleting the :data:`majority
86
- commit point <replSetGetStatus.optimes.lastCommittedOpTime>`.
100
+ Unlike other capped collections, the oplog can grow past its configured
101
+ size limit to avoid deleting the :data:`majority commit point
102
+ <replSetGetStatus.optimes.lastCommittedOpTime>`.
87
103
88
104
.. note::
89
105
90
106
MongoDB rounds the capped size of the oplog up to the nearest
91
107
integer multiple of 256, in bytes.
92
108
93
- `` _id`` Index
94
- ~~~~~~~~~~~~~
109
+ _id Index
110
+ ~~~~~~~~~
95
111
96
112
Capped collections have an ``_id`` field and an index on the ``_id``
97
113
field by default.
98
114
99
- .. _capped-collections-recommendations-and-restrictions:
100
-
101
- Restrictions and Recommendations
102
- --------------------------------
103
-
104
- Reads
105
- ~~~~~
106
-
107
- .. include:: /includes/extracts/transactions-capped-collection-read-change.rst
108
-
109
115
Updates
110
116
~~~~~~~
111
117
112
- If you plan to update documents in a capped collection, create an index
113
- so that these update operations do not require a collection scan.
114
-
115
- Sharding
116
- ~~~~~~~~
117
-
118
- You cannot shard a capped collection.
118
+ Avoid updating data in a capped collection. Because capped collections
119
+ are fixed-size, updates can cause your data to expand beyond the
120
+ collection's allocated space, which can cause unexpected behavior.
119
121
120
122
Query Efficiency
121
123
~~~~~~~~~~~~~~~~
122
124
123
- Use natural ordering to retrieve the most recently inserted elements
124
- from the collection efficiently. This is similar to using the ``tail``
125
- command on a log file.
126
-
127
- Aggregation ``$out``
128
- ~~~~~~~~~~~~~~~~~~~~
129
-
130
- The aggregation pipeline stage :pipeline:`$out`
131
- cannot write results to a capped collection.
132
-
133
- .. include:: /includes/replacement-mms.rst
134
-
135
- Transactions
136
- ~~~~~~~~~~~~
125
+ .. include:: /includes/capped-collections/query-natural-order.rst
137
126
138
- .. include:: /includes/extracts/transactions-capped-collection-change.rst
139
-
140
- Stable API
141
- ~~~~~~~~~~
127
+ Tailable Cursor
128
+ ~~~~~~~~~~~~~~~
142
129
143
- Capped collections are not supported in :ref:`Stable API
144
- <stable-api>` V1.
130
+ You can use a :term:`tailable cursor` with capped collections. Similar to the
131
+ Unix ``tail -f`` command, the tailable cursor "tails" the end of a
132
+ capped collection. As new documents are inserted into the capped
133
+ collection, you can use the tailable cursor to continue retrieving
134
+ documents.
145
135
146
- Procedures
147
- ----------
136
+ For information on creating a tailable cursor, see
137
+ :ref:`tailable-cursors-landing-page`.
148
138
149
- Create a Capped Collection
139
+ Multiple Concurrent Writes
150
140
~~~~~~~~~~~~~~~~~~~~~~~~~~
151
141
152
- You must create capped collections explicitly using the
153
- :method:`db.createCollection()` method, which is a
154
- :binary:`~bin.mongosh` helper for the :dbcommand:`create` command.
155
- When creating a capped collection you must specify the maximum size of
156
- the collection in bytes, which MongoDB will pre-allocate for the
157
- collection. The size of the capped collection includes a small amount of
158
- space for internal overhead.
142
+ .. include:: /includes/capped-collections/concurrent-writes.rst
159
143
160
- .. code-block:: javascript
161
-
162
- db.createCollection( "log", { capped: true, size: 100000 } )
163
-
164
- .. note::
165
-
166
- The value that you provide for the ``size`` field
167
- must be greater than ``0`` and less than or equal to
168
- ``1024^5`` (1 {+pb+}). MongoDB rounds the ``size`` of all capped
169
- collections up to the nearest integer multiple of 256, in bytes.
170
-
171
- Additionally, you may also specify a maximum number of documents for the
172
- collection using the ``max`` field as in the following document:
173
-
174
- .. code-block:: javascript
175
-
176
- db.createCollection("log", { capped : true, size : 5242880, max :
177
- 5000 } )
178
-
179
- .. important::
180
-
181
- The ``size`` field is *always* required, even when
182
- you specify the ``max`` number of documents. MongoDB removes older
183
- documents if a collection reaches the maximum size limit before it
184
- reaches the maximum document count.
185
-
186
- .. see::
187
-
188
- :method:`db.createCollection()` and :dbcommand:`create`.
189
-
190
- .. _capped-collections-options:
191
-
192
- Query a Capped Collection
193
- ~~~~~~~~~~~~~~~~~~~~~~~~~
194
-
195
- If you perform a :method:`~db.collection.find()` on a capped collection
196
- with no ordering specified, MongoDB guarantees that the ordering of
197
- results is the same as the insertion order.
198
-
199
- To retrieve documents in reverse insertion order, issue
200
- :method:`~db.collection.find()` along with the :method:`~cursor.sort()`
201
- method with the :operator:`$natural` parameter set to ``-1``, as shown
202
- in the following example:
203
-
204
- .. code-block:: javascript
205
-
206
- db.cappedCollection.find().sort( { $natural: -1 } )
207
-
208
- Check if a Collection is Capped
209
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210
-
211
- Use the :method:`~db.collection.isCapped()` method to determine if a
212
- collection is capped, as follows:
213
-
214
- .. code-block:: javascript
215
-
216
- db.collection.isCapped()
217
-
218
- Convert a Collection to Capped
219
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220
-
221
- You can convert a non-capped collection to a capped collection with
222
- the :dbcommand:`convertToCapped` command:
223
-
224
- .. code-block:: javascript
225
-
226
- db.runCommand({"convertToCapped": "mycoll", size: 100000});
227
-
228
- The ``size`` parameter specifies the size of the capped collection in
229
- bytes.
230
-
231
- .. include:: /includes/fact-database-lock.rst
232
-
233
- Change a Capped Collection's Size
234
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235
-
236
- .. versionadded:: 6.0
237
-
238
- You can resize a capped collection using the :dbcommand:`collMod` command's
239
- ``cappedSize`` option to set the ``cappedSize`` in bytes. ``cappedSize`` must be
240
- greater than ``0`` and less than or equal to ``1024^5`` (1 {+pb+}).
241
-
242
- .. note::
243
-
244
- Before you can resize a capped collection, you must have already set
245
- the :ref:`featureCompatibilityVersion <set-fcv>` to at least version
246
- ``"6.0"``.
247
-
248
- For example, the following command sets the maximum size of the ``"log"`` capped
249
- collection to 100000 bytes:
250
-
251
- .. code-block:: javascript
252
-
253
- db.runCommand( { collMod: "log", cappedSize: 100000 } )
254
-
255
- Change the Maximum Number of Documents in a Capped Collection
256
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
257
-
258
- .. versionadded:: 6.0
259
-
260
- To change the maximum number of documents in a capped collection, use the
261
- :dbcommand:`collMod` command's ``cappedMax`` option. If ``cappedMax`` is less
262
- than or equal to ``0``, there is no maximum document limit. If
263
- ``cappedMax`` is less than the current number of documents in the
264
- collection, MongoDB removes the excess documents on the next insert operation.
265
-
266
- For example, the following command sets the maximum number of documents in the
267
- ``"log"`` capped collection to 500:
144
+ Learn More
145
+ ----------
268
146
269
- .. code-block:: javascript
147
+ - :ref:`index-feature-ttl`
270
148
271
- db.runCommand( { collMod: "log", cappedMax: 500 } )
149
+ - :ref:`index-properties`
272
150
273
- Tailable Cursor
274
- ~~~~~~~~~~~~~~~
151
+ - :ref:`indexing-strategies`
275
152
276
- You can use a :term:`tailable cursor` with capped collections. Similar to the
277
- Unix ``tail -f`` command, the tailable cursor "tails" the end of a
278
- capped collection. As new documents are inserted into the capped
279
- collection, you can use the tailable cursor to continue retrieving
280
- documents.
153
+ .. toctree::
154
+ :titlesonly:
281
155
282
- See :doc:`/core/tailable-cursors` for information on creating
283
- a tailable cursor.
156
+ /core/capped-collections/create-capped-collection
157
+ /core/capped-collections/query-capped-collection
158
+ /core/capped-collections/check-if-collection-is-capped
159
+ /core/capped-collections/convert-collection-to-capped
160
+ /core/capped-collections/change-size-capped-collection
161
+ /core/capped-collections/change-max-docs-capped-collection
0 commit comments