What is
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.
Circular buffers are filled and emptied, so capped collection has similar behaviours.
Some notes:
- Automatic Removal of Oldest Documents
- Capped collections have an
- You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.
- You cannot shard a capped collection.
- You must create capped collections explicitly
To create a capped collection:
The name of the collection in our example is
You can also specify maximum number of documents for the collection using
Check whether collection is capped or not:
Convert a non-capped collection to capped:
#mongodb #mongo #capped #collection #capped_collection
Capped Collections
in MongoDB
?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.
Circular buffers are filled and emptied, so capped collection has similar behaviours.
Some notes:
- Automatic Removal of Oldest Documents
- Capped collections have an
_id
field and an index on the _id
field by default.- You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.
- You cannot shard a capped collection.
- You must create capped collections explicitly
To create a capped collection:
db.createCollection( "cpu_metrics", { capped: true, size: 100000 } )
The name of the collection in our example is
cpu_metrics
with the flag of capped=true
and size of the collection.You can also specify maximum number of documents for the collection using
max
as following example:db.createCollection("cpu_metrics", { capped : true, size : 5242880, max : 5000 } )
NOTE:
the size
argument is always required, even when you specify max number of documents.Check whether collection is capped or not:
db.collection.isCapped()
Convert a non-capped collection to capped:
db.runCommand({"convertToCapped": "mycoll", size: 100000});
#mongodb #mongo #capped #collection #capped_collection
Tech C**P
What is Capped Collections in MongoDB? 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…
Earlier we explained about
If you perform a
To retrieve documents in reverse insertion order, issue
#mongodb #mongo #capped_collection #natural #natural_order
capped collection
in MongoDB
. Today we just want to add something more to it.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 } )
#mongodb #mongo #capped_collection #natural #natural_order
If you go to your mongodb data directory where all database data will be stored you will see:
If you have given the size of
#mongodb #mongo #capped_collection
$ ls -lh YOUR_DATABASE.*
-rw------- 1 mongodb mongodb 64M Feb 6 07:26 YOUR_DATABASE.0
-rw------- 1 mongodb mongodb 512M Feb 6 07:26 YOUR_DATABASE.1
-rw------- 1 mongodb mongodb 16M Feb 6 07:26 YOUR_DATABASE.ns
If you have given the size of
524288000
in collection creation, then you would see 512MB for your DB size. You can also see the whole size inside of mongo shell.rs0:PRIMARY> show dbs
local 0.203GB
YOUR_DATABASE 0.578GB
#mongodb #mongo #capped_collection