1

I am doing a Mini project in MongoDB recently. I am trying to find out the distinct values of field using the db.collections.distinct(<field>)command.

The above command retrieves all the distinct values of the given field inside an array. But as I have 12k documents all the values are not getting displayed in my console.

This is the output I get.. how to view the entire result? enter image description here

I have search the internet and documentation but I was not able to get a proper answer.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
DrinkandDerive
  • 67
  • 1
  • 1
  • 7
  • is this helpful? https://stackoverflow.com/questions/3705517/how-to-print-out-more-than-20-items-documents-in-mongodbs-shell – Renato C.Francisco Jan 25 '23 at 14:45
  • @RenatoC.Francisco Nope sir, I am not sure whether the answer would work for distinct() method. More over the answer present there is for viewing an entire document.. not array values... – DrinkandDerive Jan 25 '23 at 14:49
  • Would you be willing to update your question to have the output be plain text in a code block, rather than an image? Generally, text-only images are [frowned upon](https://meta.stackoverflow.com/q/285551/1108305), unless there's something the image is showing that a pasted version of that text would be missing. If you don't want to make that change (e.g. you don't have the code handy any more), I can paste my example in from my duplicate question, since it shows the exact same phenomenon (https://stackoverflow.com/q/69399541/1108305). Just give me the go-ahead, and I'll make the change. – M. Justin Jul 02 '23 at 18:48

1 Answers1

2

The mongosh is a Node.js environment. There are several solutions, e.g. JSON.stringify the array or use util.inspect.

JSON.stringify(db.collections.distinct(<field>))

util.inspect(db.collections.distinct(<field>), { maxArrayLength: null })
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • I like that `util.inspect` appears to be the normal way things are printed in the shell (leaving out the `maxArrayLength` option gives the same truncated view). – M. Justin Jun 16 '23 at 15:41