How to get per collection last insert/update/delete time

3,354 views
Skip to first unread message

Sarthak

unread,
Feb 3, 2011, 3:59:14 PM2/3/11
to mongodb-user
Hi,
I have a need to see the timestamp of the last insert/update/delete
was executed on a given collection. Is there a utility method to see
this information?

I have tried .stats() but it does not give this information

Bernie Hackett

unread,
Feb 3, 2011, 5:03:31 PM2/3/11
to mongodb-user
You could get the last insert time by doing something like:

db.<collection>.find().sort({'_id': -1}).limit(1)

and then calling getTimestamp() on the ObjectId (assuming you are
using ObjectIds).

There isn't a good way to see last update/delete time. If you are
using replica sets you could get that from the oplog.

Sarthak

unread,
Feb 3, 2011, 5:08:58 PM2/3/11
to mongodb-user
Hi Bernie,

Thats a good workaround.. however, we have our own unique values as
_id to maintain uniqueness. So I dont have a way to do what you said.
Even my id keys is a complex hash that does not follow any particular
order.

Are there any disadvantages of doing this? Will the reads/writes
performance deteriorate as opposed to using _id? I believe there are
other ways to make sure that some field's value is unique across a
collection

Bernie Hackett

unread,
Feb 3, 2011, 6:22:27 PM2/3/11
to mongodb-user
If your dataset is small enough that your _id_ index is entirely in
memory there shouldn't be any problems. If the dataset is large there
**could** be an impact to the performance of inserts and queries
**if** your _id is random and not increasing.
Reply all
Reply to author
Forward
0 new messages