How to check mongoDB updateOne execution time

610 views Asked by At

I have been using .explain("executionStats") to find the execution time for a get-call like below in mongoDB compass console sh:

db.getCollection('Employee').find({"value.attendence":{$elemMatch:{"$lt":"2023-03-11T08:49:00Z","$gt":"2023-03-11T08:51:00Z"}}}).explain("executionStats")

This works fine.

However now I want to see the update timings and not finding a proper query. I have some various size docs to be inserted into mongo and see what is the time being taken. Tried like below no luck.

db.getCollection('Test').updateOne({
    "fid": "9999"
}, {
    "$set": {
        "fid": "9999",
        "origin":"SKD"
    }
},
{"upsert": true},
{"$explain": 1})

Above giving no details.


db.getCollection('Test').explain().updateOne({
    "fid": "9999"
}, {
    "$set": {
        "fid": "9999",
        "origin":"SKD"
    }
},
{"upsert": true})

Above gives error!

DB details:

enter image description here

db.runCommand(
   {
     explain: {
        update: "Test",
        updates: [
           {
               q: { "fid": "1111" },
               u: { "fid": "1111", "origin": "SKD" },
               upsert: true
           }
        ]
     }
   }
)

I tried the above based on the answer, it did not insert on new record or update existing. Results:

  {
  queryPlanner: {
    plannerVersion: 1,
    namespace: 'sampledb.Test',
    indexFilterSet: false,
    parsedQuery: { fid: [Object] },
    winningPlan: { stage: 'UPDATE', inputStage: [Object] },
    rejectedPlans: []
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 0,
    executionTimeMillis: 10,
    totalKeysExamined: 0,
    totalDocsExamined: 3,
    executionStages: {
      stage: 'UPDATE',
      nReturned: 0,
      executionTimeMillisEstimate: 9,
      works: 6,
      advanced: 0,
      needTime: 5,
      needYield: 0,
      saveState: 0,
      restoreState: 0,
      isEOF: 1,
      invalidates: 0,
      nMatched: 0,
      nWouldModify: 0,
      nInvalidateSkips: 0,
      wouldInsert: true,
      fastmodinsert: false,
      inputStage: [Object]
    },
    allPlansExecution: []
  },
  serverInfo: {
    host: 'middleware-mongodb-86dbd85bcc-tcmx9',
    port: 27017,
    version: '4.0.14',
    gitVersion: '1622021384533dade8b3c89ed3ecd80e1142c132'
  },
  ok: 1
}
2

There are 2 answers

3
Charchit Kapoor On

According to the official documentation, MongoDB explain function returns stats only on one update function, which is findAndModify, you should use that to measure the execution time of your updates.

FindAndModify method MongoDB.

There is one catch in using the explain function though, whenever you use explain the write operations, won't apply the changes to the database. So, it would be better if you turn on the profiling using db.setProfilingLevel() function. It will start logging your query details in the system.profile collection. You can then query this collection for query execution details.

Query Profiling in MongoDB.

10
dododo On

You should use $explain with runCommand:

let result = db.runCommand(
   {
     explain: {
        update: "products",
        updates: [
           {
               q: { fid: 999 },
               u: {    "$set": {        "fid": "9999",        "origin":"SKD"    }},
               upsert: true
           }
        ]
     },
     verbosity: "allPlansExecution"
   }
)
// print results
result["executionStats"]["executionStages"]

see for details