Monday, February 6, 2017

Capped Collection in MongoDB

Capped collections
------------------
-> Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read, and delete operations.

-> By circular, it means that when the fixed size allocated to the collection is exhausted, it will start deleting the oldest document in the collection without providing any explicit commands.


Creation:
----------
db.createCollection( "log", { capped: true, size: 100000 } )


Specify a maximum number of documents for the collection using the max field:
-----------------------------------------------------------------------------
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )


-> ordering of results is the same as the insertion order.

To retrieve documents in reverse insertion order - $natural parameter set to -1
-------------------------------------------------
db.cappedCollection.find().sort( { $natural: -1 } )


Check if a Collection is Capped:
---------------------------------
db.collection.isCapped()


Convert a Collection to Capped
-------------------------------
db.runCommand({"convertToCapped": "mycoll", size: 100000});

-> TTL Collections are not compatible with capped collections.

No comments:

Post a Comment