Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use aggreagate and want to see executionStats infomation in the explain method. #11144

Closed
byeonghun-lee opened this issue Dec 27, 2021 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@byeonghun-lee
Copy link

Hello, I tried to see executionStats at explain method.

User.aggregate()
        .match({ age: { $gt: 30 } })
        .lookup({ from: "campaign", localField: "campaign", foreignField: "_id", as: "campaign" })
        .unwind("$campaign")
        .match({ "campaign.title": "test" })
        .explain();

The result of this is:

{ stages: [ { '$cursor': [Object] }, { '$lookup': [Object] } ],
  serverInfo:
   { host: '-',
     port: '-',
     version: '4.4.10',
     gitVersion: '-' },
  ok: 1,
  '$clusterTime':
   { clusterTime:
      Timestamp { _bsontype: 'Timestamp', low_: 387, high_: 1640589687 },
     signature: { hash: [Binary], keyId: [Long] } },
  operationTime:
   Timestamp { _bsontype: 'Timestamp', low_: 387, high_: 1640589687 } }

But if I use aggregate in mongo shell I can see executionStats;

$db.user.explain("executionStats").aggregate([{$match: {age: {$gt: 30}, ....])
{queryPlanner: ... , executionStats: ...}

Is there any way to see the executionStats when using explain in monoose like the mongo shell?

node: v10
mongoose : v5.8.9

@byeonghun-lee byeonghun-lee changed the title How can i use aggreagate and want to see executionStats infomation in the explain method. How can I use aggreagate and want to see executionStats infomation in the explain method. Dec 27, 2021
@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Dec 27, 2021
@Uzlopak
Copy link
Collaborator

Uzlopak commented Dec 29, 2021

Yes. As you would do in the mongo shell

User.aggregate()
        .match({ age: { $gt: 30 } })
        .lookup({ from: "campaign", localField: "campaign", foreignField: "_id", as: "campaign" })
        .unwind("$campaign")
        .match({ "campaign.title": "test" })
        .explain("executionStats");

@vkarpov15 vkarpov15 added this to the 6.1.7 milestone Dec 30, 2021
@vkarpov15
Copy link
Collaborator

The below script includes both queryPlanner and executionStats. Make sure that there's data in the collection you're aggregating on: a common mistake is that explain() returns an object like the one you specified, no queryPlanner or executionStats, if you're aggregating on a collection that doesn't exist.

'use strict';
  
const mongoose = require('mongoose');

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');

  await mongoose.connection.dropDatabase();

  const User = mongoose.model('User', Schema({ name: String }));

  await User.create({ name: 'test' });

  const res = await User.aggregate([{ $match: { name: 'test' } }])
        .explain();

  console.log(res);
}

Output:

{
  queryPlanner: {
    plannerVersion: 1,
    namespace: 'test.users',
    indexFilterSet: false,
    parsedQuery: { name: [Object] },
    optimizedPipeline: true,
    winningPlan: { stage: 'COLLSCAN', filter: [Object], direction: 'forward' },
    rejectedPlans: []
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 1,
    executionTimeMillis: 0,
    totalKeysExamined: 0,
    totalDocsExamined: 1,
    executionStages: {
      stage: 'COLLSCAN',
      filter: [Object],
      nReturned: 1,
      executionTimeMillisEstimate: 0,
      works: 3,
      advanced: 1,
      needTime: 1,
      needYield: 0,
      saveState: 0,
      restoreState: 0,
      isEOF: 1,
      direction: 'forward',
      docsExamined: 1
    },
    allPlansExecution: []
  },
  serverInfo: {
    host: 'localhost',
    port: 27017,
    version: '4.4.0',
    gitVersion: '563487e100c4215e2dce98d0af2a6a5a2d67c5cf'
  },
  ok: 1
}

That being said, it does look like we don't support passing a verbosity param to explain(), so we'll add that for the next release.

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Jan 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants