Skip to content

Commit 59a85f9

Browse files
committed
Capped Collection
1 parent 1cdbb55 commit 59a85f9

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,33 @@ With this approach, we will need two queries: first to fetch the `address_ids` f
16141614
<b><a href="#">↥ back to top</a></b>
16151615
</div>
16161616
1617-
#### Q. ***What is use of capped collection in MongoDB?***
1617+
## Q. ***What is use of capped collection in MongoDB?***
1618+
1619+
**Capped collections** are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to `circular buffers`: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.
1620+
1621+
Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the order of the disk storage, it ensures that the document size does not increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or any other high volume data.
1622+
1623+
**Example:-**
1624+
1625+
```js
1626+
>db.createCollection( "log", { capped: true, size: 100000 } )
1627+
1628+
1629+
// specify a maximum number of documents for the collection
1630+
>db.createCollection("log", { capped: true, size: 5242880, max: 5000 } )
1631+
1632+
1633+
// check whether a collection is capped or not
1634+
>db.cappedLogCollection.isCapped()
1635+
1636+
1637+
// convert existing collection to capped
1638+
>db.runCommand({"convertToCapped": "posts", size: 10000})
1639+
1640+
1641+
// Querying Capped Collection
1642+
>db.cappedLogCollection.find().sort({$natural: -1})
1643+
```
16181644
16191645
<div align="right">
16201646
<b><a href="#">↥ back to top</a></b>

0 commit comments

Comments
 (0)