9

I am trying to find out the distinct values of a 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 do I view the entire result?

> db.dummy.distinct("dineRatingCount")
[
  null,      '6391573', '7780279', '6434564', '6761878', '3310772',
  '7347435', '2657046', '1054165', '6351703', '3400666', '8380233',
  '9400183', '5708289', '4303540', '5921412', '3596752', '6677940',
  '5641409', '8683847', '2874620', '2392159', '6405989', '6682474',
  '9453088', '9335486', '8688344', '8036721', '9090412', '6045969',
  '5640223', '6820181', '9748932', '7040773', '3683845', '4162813',
  '7538482', '2764420', '5630659', '6304861', '5628430', '8011166',
  '1912403', '8855139', '8861703', '4175488', '6180727', '4914295',
  '2904422', '1187556', '4707649', '7010787', '5239511', '5149672',
  '2180740', '9855302', '6133666', '3072308', '7600516', '8670256',
  '1390702', '3632377', '5251961', '4552278', '6606445', '6243124',
  '1812204', '2213617', '2564475', '1788878', '5261363', '6731404',
  '8514847', '9289278', '7091775', '5936085', '4796291', '2735993',
  '1188367', '8907823', '5307456', '6924707', '7127922', '9730023',
  '8811050', '1113740',
  ... 302 more items
]

I have searched the internet and documentation but I was not able to find an answer.

2
  • is this helpful? stackoverflow.com/questions/3705517/… Commented Jan 25, 2023 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... Commented Jan 25, 2023 at 14:49

1 Answer 1

12

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 })
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.