【问题标题】:How to use explain() in Node.js mongoose如何在 Node.js mongoose 中使用 explain()
【发布时间】:2022-02-10 01:36:01
【问题描述】:

我试图在我的控制台上查看 executionStats,但我总是得到 undefined[object, object]。我只是有一个简单的查询,我想在其中查看一些统计信息。有人可以解释为什么这不起作用吗?如果我只使用我的查询进行查询(而不使用explain()),则查询成功。

这是我在尝试将解释与所有其他代码一起使用时的查询。我想查看查询的性能,所以我发现获得性能时间的最佳方法是使用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.`);
  });

这是我的查询代码,我可以在其中成功获取数据并在我的页面上显示它们

  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.`);
  });

这是我的 MongoDB 调试输出:

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 })
}

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您需要更改将解释对象记录到控制台的方式。

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

    通过使用'MongoDebug: ' + explain[0],您在explain[0] 上隐式调用.toString(),因为您将它附加到一个字符串,所以它必须是一个字符串。对于对象,这会将它们转换为[object, Object]

    相反,您可以将其作为单独的对象传递给您的 console.log:

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

    或将其转换为带有额外间距的 JSON 以获得更好的可读性:

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

    【讨论】:

    • 感谢您的帮助。完全忘记了我将它作为字符串附加。这有帮助。我还删除了“[0]”,否则我再次得到undefined。还有一个快速的问题。我在控制台中完成了整个调试。有没有办法让我只获取我在查询中指定的 executionStats?
    • @Anzeko 你得到了什么多余的数据?您编写查询.explain("executionStats" 的方式应该没问题。你能告诉我你得到的输出吗?
    • 我发现如果它显示所有信息就可以了,所以这里没有问题。很抱歉浪费您的时间,谢谢。如果您想查看它,我会更新我的帖子。
    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 2015-10-29
    • 1970-01-01
    • 2017-05-14
    • 2011-08-14
    • 1970-01-01
    • 2017-11-23
    相关资源
    最近更新 更多