Capped Collections:
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.
Procedures
→ Create a Capped Collection
You must create capped collections explicitly using the db.createCollection() method, which is a helper in the mongo shell for the create command. When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection. The size of the capped collection includes a small amount of space for internal overhead.
→ Query a Capped Collection
If you perform a find() on a capped collection with no ordering specified, MongoDB guarantees that the ordering of results is the same as the insertion order.
To retrieve documents in reverse insertion order, issue find() along with the sort() method with the $natural parameter set to -1, as shown in the following example:
db.cappedCollection.find().sort( { $natural: -1 } ) |
→ Check if a Collection is Capped
Use the isCapped() method to determine if a collection is capped, as follows:
db.collection.isCapped() |
→ Convert a Collection to Capped
You can convert a non-capped collection to a capped collection with the convertToCapped command:
db.runCommand({"convertToCapped": "mycoll", size: 100000}); |
The size parameter specifies the size of the capped collection in bytes.
0 Comments