Skip to content

Commit b5cde9f

Browse files
DOCSP-38000 Merge Capped Collections IA feature branch into master (#7033) (#7036)
* DOCS-15096 Capped collections landing page updates (#6079) * DOCS-15096 Capped collections landing page updates * move capped collections page and update snooty.toml * add facet * formatting * reorg * fix ref * update use cases * review edits * shuffle * wording * address review comments * paragraph split * typo * undo toc update * following > these * review edits * typo * wording * clarify update note * edit * (DOCSP-37995): Change max docs for capped collection (#6948) * (DOCSP-37995): Change max docs for capped collection * edit include * add ref link * add comma * (DOCSP-38002): Change byte size of a capped collection (#6953) * (DOCSP-38002): Change byte size of a capped collection * add unit * fix command * fix command * (DOCSP-38010): convert collection to capped (#6955) * (DOCSP-38010): convert collection to capped * typo * add copyable option * change learn more link * build error * add detail for db lock * edits * remove quotes around field name * (DOCSP-38019): Check if collection is capped (#6957) * (DOCSP-38019): Check if collection is capped * fix formatting for collection names * formatting * formatting * add page meta info * fix indentation * (DOCSP-38041): Create a capped collection (#6965) * (DOCSP-38041): Create a capped collection * edits * formatting * review edits * (DOCSP-38027): Query a capped collection (#6962) * (DOCSP-38027): Query a capped collection * add code block language * edits * add page metadata * typo * add natural order and use include * edits * formatting * add learn more * add step lead-in * review edits * add detail for multiple concurrent writes * add period * nit
1 parent 7a3826f commit b5cde9f

11 files changed

+649
-216
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ toc_landing_pages = [
4646
"/core/authentication",
4747
"/core/authorization",
4848
"/core/backups",
49+
"/core/capped-collections",
4950
"/core/crud",
5051
"/core/csfle",
5152
"/core/csfle/fundamentals/",

source/core/capped-collections.txt

Lines changed: 94 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.. _manual-capped-collection:
2+
.. _capped_collections_remove_documents:
23

34
==================
45
Capped Collections
@@ -12,66 +13,81 @@ Capped Collections
1213
:depth: 2
1314
:class: singlecol
1415

15-
Overview
16-
--------
16+
.. facet::
17+
:name: genre
18+
:values: reference
1719

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

25-
See :method:`~db.createCollection()` or :dbcommand:`create`
26-
for more information on creating capped collections.
26+
Restrictions
27+
------------
2728

28-
.. tip::
29+
- Capped collections cannot be sharded.
2930

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/>`.
3533

36-
TTL indexes are not compatible with capped collections.
34+
- Capped collections are not supported in :ref:`Stable API <stable-api>`
35+
V1.
3736

37+
- You cannot write to capped collections in :ref:`transactions
38+
<transactions>`.
3839

39-
Behavior
40-
--------
40+
- The :pipeline:`$out` aggregation pipeline stage cannot write results
41+
to a capped collection.
4142

42-
Insertion Order
43-
~~~~~~~~~~~~~~~
43+
- You cannot use read concern :readconcern:`"snapshot"` when reading
44+
from a capped collection.
4445

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

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

52-
Automatic Removal of Oldest Documents
53-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
To create and query capped collections, see these pages:
5472

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`
5874

59-
Consider the following potential use cases for capped
60-
collections:
75+
- :ref:`capped-collections-query`
6176

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`
6978

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

7692
.. _capped-collections-oplog:
7793

@@ -81,203 +97,65 @@ Oplog Collection
8197
The :term:`oplog.rs <oplog>` collection that stores a log
8298
of the operations in a :term:`replica set` uses a capped collection.
8399

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

88104
.. note::
89105

90106
MongoDB rounds the capped size of the oplog up to the nearest
91107
integer multiple of 256, in bytes.
92108

93-
``_id`` Index
94-
~~~~~~~~~~~~~
109+
_id Index
110+
~~~~~~~~~
95111

96112
Capped collections have an ``_id`` field and an index on the ``_id``
97113
field by default.
98114

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-
109115
Updates
110116
~~~~~~~
111117

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

120122
Query Efficiency
121123
~~~~~~~~~~~~~~~~
122124

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
137126

138-
.. include:: /includes/extracts/transactions-capped-collection-change.rst
139-
140-
Stable API
141-
~~~~~~~~~~
127+
Tailable Cursor
128+
~~~~~~~~~~~~~~~
142129

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

146-
Procedures
147-
----------
136+
For information on creating a tailable cursor, see
137+
:ref:`tailable-cursors-landing-page`.
148138

149-
Create a Capped Collection
139+
Multiple Concurrent Writes
150140
~~~~~~~~~~~~~~~~~~~~~~~~~~
151141

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
159143

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

269-
.. code-block:: javascript
147+
- :ref:`index-feature-ttl`
270148

271-
db.runCommand( { collMod: "log", cappedMax: 500 } )
149+
- :ref:`index-properties`
272150

273-
Tailable Cursor
274-
~~~~~~~~~~~~~~~
151+
- :ref:`indexing-strategies`
275152

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

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

Comments
 (0)