0% found this document useful (0 votes)
22 views14 pages

Understanding MongoDB Capped Collections

Uploaded by

sumathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

Understanding MongoDB Capped Collections

Uploaded by

sumathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Capped Collection

Capped collections are fixed-size collections that insert and


retrieve documents based on insertion order. Capped
collections work similarly to circular buffers: once a collection
fills its allocated space, it makes room for new documents by
overwriting the oldest documents in the collection.
Benefits of using capped collections:
• Capped collections guarantee that the insertion order is preserved. Queries do
not need to use an index to return documents in the order they were stored, thus
eliminating indexing overhead.
• Capped collections also guarantee that the insertion order is identical to the
order on disk by prohibiting updates that increase the document size. This
eliminates the overhead of relocating and managing the new location of
documents.

• Capped collections automatically remove the oldest documents in the collection.

Therefore, you do not need to implement deletion in your application code.


• Documents cannot be updated to a larger size once they have been inserted into the
capped collection. You update them, but the data must be the same size or smaller.
• Documents cannot be deleted from a capped collection. That means that the data
takes up space on disk even if it is not being used. You can explicitly drop the capped
collection to effectively delete all entries.
• Capped collections cannot be sharded.
• You cannot create capped collections on serverless instances.
• Capped collections are not supported in Stable API V1.
• You cannot write to capped collections in transactions.
• The $out aggregation pipeline stage cannot write results to a capped collection.
Query a Capped Collection

• When you query a capped collection without


specifying a sort order, MongoDB returns
results in the same order that they were
inserted, meaning the oldest documents are
returned first.
• Use natural ordering to retrieve the most
recently inserted elements from the collection
efficiently. This is similar to using
the tail command on a log file
[Link]( [
{
message: "system start",
type: "startup",
time: 1711403508
},
{
message: "user login attempt",
type: "info",
time: 1711403907
},
{
message: "user login fail",
type: "warning",
time: 1711404209
},
{
message: "user login success",
type: "info",
time: 1711404367
},
{
message: "user logout",
type: "info",
time: 1711404555
}
])
Return Documents in Insertion Order

• Query the log collection for documents where type is info, and use the
default sort order:
Return Most Recent Documents
• To return documents in reverse insertion order (meaning the most recent
documents are first), specify the sort() method with the $natural parameter set
to -1.
Check if a Collection is Capped

• To check if a collection is capped, use the


isCapped() method.
Convert a Collection to Capped

• To convert a non-capped collection to a capped collection, use the


convertToCapped database command.
Change the Size of a Capped Collection

• To change the size of a capped collection, the collMod


command's cappedSize option. cappedSize is specified in bytes, and must be
greater than 0 and less than or equal to 1024^5 (1 PB).
• if cappedSize is less than the current size of the collection, MongoDB removes the
excess documents on the next insert operation.
• [Link]( { collMod: "log", cappedSize: 5242880 } )
• To change the maximum number of documents in a capped collection, use the
collMod command's cappedMax option.
• If cappedMax is less than or equal to 0, there is no maximum document limit.
• If cappedMax is less than the current number of documents in the collection,
MongoDB removes the excess documents on the next insert operation.

• [Link]( { collMod: "log", cappedMax: 5000 } )


General
• TTL (Time To Live) indexes offer better performance
and more flexibility than capped collections. TTL
indexes expire and remove data from normal
collections based on the value of a date-typed field
and a TTL value for the index.
• Capped collections serialize write operations and
therefore have worse concurrent insert, update, and
delete performance than non-capped collections.
Before you create a capped collection, consider if
you can use a TTL index instead.
Reference
• [Link]
core/capped-collections/

You might also like