0

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?

1
  • Do you have any field in your records, which is updated with every update and insert? Commented Jun 10, 2016 at 8:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.