Command response depth

So I have been stuck on this problem for a while. I have just started exploring mongodb and am trying to figure out loads of things. So I wanted to use the command “listCollections” to see things like the validators. But when I do this It doesn’t even show the firstBatch and only shows me that there is an Object. I was able to solve this problem in the shell (mongosh) with the “inspectDepth” configuration but I can’t find a solution in Node with the MongoDB driver.

So I have this:

  let result = db.command({
     listCollections: 1,
  result = Promise.resolve(result).then(result => console.log(result.firstBatch));

And as a response I get this:

I would like it to show the whole Object (and all nested Objects) instead of just telling me that there is an Object. Can someone help?

As opposed to console.log try JSON.stringify

That seems to have done the Job. Does that imply that MongoDB provides the data and the terminal just happens to format it that way (i.e. leave out the actual Object)?

Console.log will not output a nested structure well, you can also format it a touch better with

JSON.stringify(theVariable, null, 2)

Where 2 is the indent level.

Of course if you know the structure you can create logic to output in a cusom format.

1 Like

Edit: I just found a second way. I don’t know if this i elegant but one could also use console.dir(Object, {depth: null}) .

Excellent, shall also try that next time I need to do that.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.