There are multiple databases but some of them look stale. I need to understand if the database is stale or not.

How can I get the last actions executed of a specific database in MongoDB? And can I get the date and time information of the last actions?

  • ,What is MongoDB version(x,y,z)? – Md Haidar Ali Khan May 24 '18 at 5:10
  • 2.4 but we have different VMs with other Mongos such as 3.2, 3.4 and 3.6 – Yasin Okumuş May 24 '18 at 16:56

If you want a quick way to check if a database is actively being used, you can use the top administrative command. This command provides usage statistics with collection-level granularity.

For example, using the mongo shell to check if a twitter.tweets collection is being actively queried:

>  db.adminCommand('top').totals["twitter.tweets"].queries
{ "time" : 1414008, "count" : 23779 }

The time value is in microseconds and the count indicates how many operations were observed since this mongod last restarted. For an unused collection both of these values would be low or 0.

You can sample top metrics over time to see if counters continue to increment due to usage.

If you want to check which commands are being run against a database, enable the Database Profiler with profiling level 2 (all operations):

> use mydb
switched to db mydb

> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }

This will enabling logging of all recent queries and commands for the current database in a system.profile capped collection. You can then count or find recent operations by querying the system.profile collection.

There are a number of fields in the system.profile documents that may be helpful to match on, such as: op (type of operation), ts (timestamp), and client (IP address or hostname where an operation originated. At a minimum you'll probably want to match on the ns (namespace) value, because profiling level 2 will also include queries against the system.profile collection.

A capped collection has the helpful property of guaranteeing insertion order, so a sort by $natural order can be used to find oldest or newest profile documents.

A few examples:

// Count the number of recent operations for twitter.tweets
> db.system.profile.find({ns:'twitter.tweets'}).count()

// Find the latest query profiled for twitter.tweets
> db.system.profile.find(

As per MongoDB documentation here through getLog is an administrative command that returns the most recent 1024 logged mongod events.

The getLog does not read log data from the mongod log file. It instead reads data from a RAM cache of logged mongod events. To run getLog, use the db.runCommand( { command } ) method on the admin database.

The getLog command has the following syntax:

{ getLog: <value> }

The possible values for getLog are:

  • global - returns the combined output of all recent log entries.
  • rs - if the mongod is part of a replica set, getLog returns recent notices related to replica set activity.
  • startupWarnings - returns logs that may contain errors or warnings from MongoDB’s log from when the current process started. If mongod started without warnings, this filter may return an empty array.

Retrieve Recent Events from Log

The following operation retrieves the most recent global events for the mongod

db.adminCommand( { getLog : "global" } )

For your further ref here

  • Thank you but it does not give information per database. It gives the log of all dbs. Not a specific db. If there is a parameter for db, I would appreciate the way how to get that. – Yasin Okumuş May 24 '18 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.