Reputation: 31
I want to delete all the collections from my db which are not used for long time. Is there any why i can check when the particular collection was last used?
Upvotes: 0
Views: 92
Reputation: 5652
It depends what you mean by 'last used'. If you mean the last time a document was inserted into the collection then you could do this by converting the ObjectId of the last inserted document into a date. The following query should return the date the last document was inserted:
db.<collection_name>.findOne({},{_id:1})._id.getTimestamp()
the findOne query will return documents in natural order, therefore if you input no query criteria ('{}
') then it will return the most recently inserted document. You can then get the _id
field and call the getTimestamp()
function
I'm not sure if there is any way to reliably tell when a collection was last queried. If you're running your database with profiling enabled then there might be entries in the db.system.profile collection, or in the oplog.
Upvotes: 1