1

Anyone have a handy mongo command to remove all entries from a DB that are older than X date/ X days?

Basically have a dev and production DB, I'm looking to prune the dev DB out a bit to limit size.

Thanks for the help!

AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
  • They **mostly** have datetime fields. Let's assume they all do for brevity's sake. I can always sort it by _id and only keep the last X documents if I need to. – AlbertEngelB Apr 01 '13 at 21:30

2 Answers2

3

You could do something like this from the mongo shell.

var older=Date.parse("2013-03-01"),collection=db.so,all=collection.find();
all.forEach(function(doc) { var ts = doc._id.getTimestamp();
    if (ts < older) { collection.remove(doc); } });

The above line (which you'd paste into the shell) will delete all documents in the specified collection (collection=db.so) created before the first of March, 2013. It relies on the fact that each ObjectId has an embedded timestamp (based on the timestamp of document creation (docs)), which can be retrieved and used.

You could of course change the query to look for a specific timestamp field in a document.

if (doc.timestampField < older) { collection.remove(doc); } }) 
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • It did crash my MongoDB instance when I ran this, however I'm 95% sure this will work as advertised. I'll run this tonight when traffic is low and see how she goes. Thanks! – AlbertEngelB Apr 02 '13 at 15:12
  • You might want to move the logic to a small app written in your favorite programming language. There seems to be a limit to the number of lines/complexity of the mongodb shell JS support, so I couldn't do much with it, nor add any robustness. I couldn't get it to crash, but my developer workstation didn't have much data that I could delete. – WiredPrairie Apr 02 '13 at 15:43
  • Yeah, better idea for sure. I should be able to whip something up pretty easily, the only thing I wonder about is the _id.getTimestamp(). Looking at this answer: http://stackoverflow.com/questions/6452021/getting-timestamp-from-mongodb-id I don't think it will be a problem! – AlbertEngelB Apr 02 '13 at 15:54
2

You can use the mongo concept of deleting data after some specified amount of time, Expire Data from Collections by Setting TTL.

Please refer below link to do so http://docs.mongodb.org/manual/tutorial/expire-data/

sandy
  • 21
  • 1
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Shog9 Nov 12 '13 at 00:39