Home > Mobile >  How to use explain() in Node.js mongoose
How to use explain() in Node.js mongoose

Time:02-10

I am trying to see The executionStats on my console but I always get undefined or [object, object]. I just have a simple query where I want to see some stats. Can someone explain why this isn't working? If I just use my query for querying (while not using explain()) the query is successful.

This is my query when trying to use explain with all the other code. I want to see the performance of the query so I found out that the best way to get the performance time is by using explain().

  const timeFunction2 = new Promise((resolve, reject) => {
    var startTime = performance.now();

    setTimeout(() => {

      conn.collection('galery').find({ "user_id": req.session.userId}).explain("executionStats", (err, explain) => {
        console.log('MongoDebug: '   explain[0]);
      });

      var endTime = performance.now();
      resolve(endTime - startTime);
    });
  });

  timeFunction2.then(time => {
    console.log(`${time} ms.`);
  });

And this is my query code where I can sucssessfuly get data and show them on my page

  const timeFunction2 = new Promise((resolve, reject) => {
    var startTime = performance.now();

    setTimeout(() => {


conn.collection('galery').find({ "user_id": req.session.userId}).toArray( (err, resultImg) =>{
.
.
.
});

      var endTime = performance.now();
      resolve(endTime - startTime);
    });
  });

  timeFunction2.then(time => {
    console.log(`${time} ms.`);
  });

This is my output of MongoDB Debug:

MongoDebug:  {
  queryPlanner: {
    plannerVersion: 1,
    namespace: 'nodejsnosql.galerija',
    indexFilterSet: false,
    parsedQuery: { uporabnik_id: [Object] },
    winningPlan: { stage: 'COLLSCAN', filter: [Object], direction: 'forward' },
    rejectedPlans: []
  },
  executionStats: {
    executionSuccess: true,

    nReturned: 3,
    executionTimeMillis: 0,
    totalKeysExamined: 0,
    totalDocsExamined: 5,
    executionStages: {
      stage: 'COLLSCAN',
      filter: [Object],
      nReturned: 3,
  ok: 1,
  '$clusterTime': {
    clusterTime: new Timestamp({ t: 1644428067, i: 1 }),
    signature: {
      hash: new Binary(Buffer.from("71a216b111009bfcef6bd4923029d8df7a219e1a", "hex"), 0),
      keyId: new Long("7027534341366349828")
    }
  },
  operationTime: new Timestamp({ t: 1644428067, i: 1 })
}

CodePudding user response:

You need to change how you are logging the explain object to console.

      conn.collection('galery').find({ "user_id": req.session.userId}).explain("executionStats", (err, explain) => {
        console.log('MongoDebug: '   explain[0]);
      });

By using 'MongoDebug: ' explain[0] you are implicitly calling .toString() on explain[0] as you are appending it to a string, so it must be a string. For objects, this converts them to [object, Object].

Instead you can pass it as a separate object to your console.log:

console.log('MongoDebug: ', explain);

Or convert it to JSON with extra spacing for better readability:

console.log(JSON.stringify(explain, null, 2));
  • Related