What's the easiest way to delete records older than X days from the command-line?

4,790 views
Skip to first unread message

dblock

unread,
May 24, 2011, 1:51:14 PM5/24/11
to mongodb-user
What's the easiest way to delete records older than X days from the
command-line? I tried some clever javascript with --eval but couldn't
get a date comparison to work.

Robert Stam

unread,
May 24, 2011, 2:40:56 PM5/24/11
to mongodb-user
What do your documents look like? Is there a timestamp in there that
can be used to figure out how old a record is?

If not, if you are using ObjectId for your _id field, some of the
bytes of the ObjectId are a timestamp, so from an ObjectId you can get
a pretty good approximation of when a document was created.

Can you show us a sample document?

Are you using the mongo shell?

dblock

unread,
May 24, 2011, 7:39:42 PM5/24/11
to mongodb-user
These have updated_at.

{
"_id" : ObjectId("4d8b92c34eb68a1b2c000497"),
"created_at" : ISODate("2011-03-24T18:51:47Z"),
"name" : "Foobar",
"updated_at" : ISODate("2011-05-24T01:09:23Z"),
}

My problem is more of a syntax one between <strike>command-line &
javascript</strike> the computer and the chair ;)

Nat

unread,
May 24, 2011, 8:56:58 PM5/24/11
to mongod...@googlegroups.com
It would probably be a lot easier if you write a short script in scripting language like perl, python, ruby. It gives you with a flexibility to your archiving logic.
-----Original Message-----
From: dblock <dblock...@gmail.com>
Sender: mongod...@googlegroups.com
Date: Tue, 24 May 2011 16:39:42
To: mongodb-user<mongod...@googlegroups.com>
Reply-To: mongod...@googlegroups.com
Subject: [mongodb-user] Re: What's the easiest way to delete records older
than X days from the command-line?

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

dblock

unread,
May 25, 2011, 9:34:27 AM5/25/11
to mongodb-user
It's part of some shell scripts that use mongodump/mongorestore.
Adding Ruby/Perl/Foobar to the loop would add a whole other setup
problem to this quite simple set. I wish mongo shell could just
execute a command and terminate instead of having clumsy javascript,
but I would be happy with javascript to do it.

Robert Stam

unread,
May 25, 2011, 12:51:58 PM5/25/11
to mongodb-user
The --eval command line option gets awkward when the JavaScript is non-
trivial. But you can execute a longer script stored in a file. The
file must be pure JavaScript, so shell commands like "use dbname"
don't work, so you must specify the database to use in the command
line args. For example:

> mongo --host hostname --port portnumber dbname script.js

or

> mongo hostname:portnumber/dbname script.js

will connect to the server at hostname:portnumber and execute the
script stored in the file "script.js".

When running the script it doesn't echo the results of each line to
standard output the way the shell does when you run it in interactive
mode, so you can debug the script in interactive mode first.

As far as the remove statement itself goes, wouldn't it just be
something like:

> var cutoff = ISODate("2011-05-20")
> db.test.remove({updated_at : {$lt : cutoff}})
Reply all
Reply to author
Forward
0 new messages