snippetMinor
How to see last actions executed (and time) of a specific database in MongoDB?
Viewed 0 times
lastactionsmongodbseetimedatabaseexecutedhowandspecific
Problem
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?
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?
Solution
If you want a quick way to check if a database is actively being used, you can use the
For example, using the
The
You can sample
If you want to check which commands are being run against a database, enable the Database Profiler with profiling level 2 (all operations):
This will enabling logging of all recent queries and commands for the current database in a
There are a number of fields in the
A capped collection has the helpful property of guaranteeing insertion order, so a sort by
A few examples:
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(
{ns:'twitter.tweets','op':'query'}
).sort({$natural:-1}).limit(1).pretty()Code Snippets
> db.adminCommand('top').totals["twitter.tweets"].queries
{ "time" : 1414008, "count" : 23779 }> use mydb
switched to db mydb
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }// 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(
{ns:'twitter.tweets','op':'query'}
).sort({$natural:-1}).limit(1).pretty()Context
StackExchange Database Administrators Q#207629, answer score: 3
Revisions (0)
No revisions yet.