TTL Index: - Time To Live
----------
-> TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time.
Create TTL:
------------
-> Ex: To create a Index which deletes the data more than 6 months.
db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 1.577e+7 } )
collMod - to modify TTL index
------------------------------
db.runCommand( { collMod: "sessions",
index: { keyPattern: { lastAccess: 1 },
expireAfterSeconds: 3600
}
})
o/p:
----
{ "expireAfterSeconds_old" : 1800, "expireAfterSeconds_new" : 3600, "ok" : 1 }
To see all the Indexes in a Coll
---------------------------------
db.people.getIndexes()
To see Indexes in a entire DB
------------------------------
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
Deleting a Index
------------------
db.pets.dropIndex( "catIdx" )
db.pets.dropIndex( { "cat" : -1 } )
db.collection.dropIndexes()
----------
-> TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time.
Create TTL:
------------
-> Ex: To create a Index which deletes the data more than 6 months.
db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 1.577e+7 } )
collMod - to modify TTL index
------------------------------
db.runCommand( { collMod: "sessions",
index: { keyPattern: { lastAccess: 1 },
expireAfterSeconds: 3600
}
})
o/p:
----
{ "expireAfterSeconds_old" : 1800, "expireAfterSeconds_new" : 3600, "ok" : 1 }
To see all the Indexes in a Coll
---------------------------------
db.people.getIndexes()
To see Indexes in a entire DB
------------------------------
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
Deleting a Index
------------------
db.pets.dropIndex( "catIdx" )
db.pets.dropIndex( { "cat" : -1 } )
db.collection.dropIndexes()
No comments:
Post a Comment