How to profile Mongo DB

In the Mongo Client, activate the profiler:


After some requests are done, list them:

db.system.profile.find({'query': {$exists: true}, 'ns': {$not:/.system.profile/}}, {'ts':1, 'ns': 1, 'query': 1, 'updateobj': 1, 'appName': 1}).limit(20).sort({ts: -1}).pretty();

You can also define a js function to ease the profiler’s usage

function getLastQueries(limit, appName) {
    let query = {'query': {$exists: true}, 'ns': {$not:/\.system\.profile/}}
    let projection = {'ts':1, 'ns': 1, 'query': 1, 'updateobj': 1, 'appName': 1}

    if (limit === undefined) limit = 3
    if (appName !== undefined) query.appName = appName

    return db.system.profile.find(query, projection).limit(limit).sort({ts: -1}).pretty()
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy