[MongoDB] How to Check Execution Time of aggregate

Tadashi Shigeoka ·  Tue, December 16, 2014

I’ll introduce a command to check the execution time of db.collection.aggregate() in mongo shell.

MongoDB | モンゴディービー

Check execution time with time mongo dbname --eval 'query'

I used the time command from bash or zsh to pass a query via mongo —eval and check the execution time.

$ time mongo dbname --eval 'db.orders.aggregate([ { $match: { customer_id: "abcdefg", payment_system: "paypal" } } , { $group: { _id: "$user" } } ])'
MongoDB shell version: 2.6.5
connecting to: dbname
[object Object]

real  0m0.120s
user  0m0.044s
sys  0m0.004s

Doesn't the explain option for aggregate work?

You can specify explain as a Boolean in the second argument of db.collection.aggregate(pipeline, options).

However, this seems a bit different from the execution results of db.collection.find().explain(), and I couldn’t check execution time.

  db.users.aggregate( [ ], { explain: true } )
{
  "stages" : [
    {
      "$cursor" : {
        "query" : {
          
        },
        "plan" : {
          "cursor" : "BasicCursor",
          "isMultiKey" : false,
          "scanAndOrder" : false,
          "allPlans" : [
            {
              "cursor" : "BasicCursor",
              "isMultiKey" : false,
              "scanAndOrder" : false
            }
          ]
        }
      }
    }
  ],
  "ok" : 1
}

If you know a method other than using the time command, please let me know.

Reference Information

That’s all from the Gemba.