Wednesday, August 15, 2018

11. MongoDB : CAP COLLECTION & TTL Index.

11. MongoDB : CAP COLLECTION & TTL Index.


CAP COLLECTION - Create collection with fixed size. ######################################

Why we need this kind of collections ...? 
- Eg: Server Log - we dont want to keep historical data, as soon as new data comes in old data has to delete.

//max size of 5242880 bytes, and 5000 documents
db.createCollection("webserverlog", {capped:true,size:5242880,max:5000});
show collections
db.webserverlog.isCapped()
db.runCommand({"convertToCapped":"items",size:100000});  //to convert to cap collection
db.items.isCapped()





//How to delete the documents
TTL Index (Time to live) ###########################################
-----------------------------
//document will be deleted after (createdDate + 3600 sec), eg: ('May 30, 2015 14:00:00' + 3600 sec)
db.webserverlog.createIndex({"createdDate":1},{expireAfterSeconds:3600});

//document will be deleted after (expireDate + 0 sec), eg: ('June 30, 2015 14:00:00' + 0 sec)
db.webserverlog.createIndex({"expireDate":1},{expireAfterSeconds:0});
db.webserverlog.insert({"expireDate":new Date('June 30, 2015 14:00:00')});



No comments:

Post a Comment