How to find the last update time of a collection and database

My website used a lot of data from Mongodb, sometimes if my database/collection didn’t update for a while, it means that my system is probably broken, therefore I want to get the last update (document) time of a database and collection, how can I get it accurately?

Hey @WONG_TUNG_TUNG,

Welcome to the MongoDB Community!

May I ask what specifically you mean by “system is probably broken”? Could you please clarify this more in order to assist you better?

Additionally, also share the MongoDB version you are using and where it is deployed, is it MongoDB Atlas or on-prem?

Regards,
Kushagra

If you just want to check whether there is any updating operation occasionally, db.currentOp() can help you find the active connections to MongoDB and what they are doing. Since the output of db.currentOp() is long and lengthy, you can define your own function to capture only the content you are interested in:

db.currentOp().inprog.forEach(
  function(c) {
    print(" client: ", c.client);
    print(" appName: ", c.appName);
    print(" active: ", c.active);
    print(" currentOpTime: ", c.currentOpTime);
    print(" op: ", c.op);
    print(" ns: ", c.ns);
    print(" command: ", c.command);
    print("= = = = = = = = = = = \n");
  }
)