How to use explain( ) in Node

Works fine in the shell but not in node. How to make it work in node?

//DOES NOT WORK IN NODE
const result = await db.collection(‘Users’).explain().aggregate().next()

Hi @max_matinpalo, you can use .explain() method in your node.js app, but the following will never work, not even in the MongoDB shell:

You can use the following syntax to get the explain results in your result variable:

const result = await db.collection("Users").find().explain()

also,

const result = await db.collection("Users").aggregate([<---Your Pipeline--->]).explain()

I hope it helps.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like