3

I was doing some one-time manual analysis of my data using the new MongoDB shell (mongosh) to query the collections directly. One thing I needed was a list of distinct field values from a collection:

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

However, I want the full list of IDs, not a truncated list. I tried various approaches, including iterating over the list, converting to an array, or setting the display batch size, but nothing seemed to work to get me the full list.

Ultimately, I ended up using an aggregation, since I know how to convert that into an array:

db.myCollection.aggregate([{$group: {_id: "$myExternalId"}}]).map(d => d._id)

That being said, it would certainly have been more convenient if there were a way to get the full array of elements returned by distinct. Is there a way to tell the new MongoDB shell to output the full list?

M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • I'm voting to close my own question as a duplicate of https://stackoverflow.com/q/75235592/1108305. It's the exact same scenario, and even though the question was created later, it has a perfect answer that I need: https://stackoverflow.com/a/75235976/1108305. – M. Justin Jun 16 '23 at 15:42

1 Answers1

-1

Setting DBQuery.shellBatchSize = 1000; worked for me. Any number bigger than your array's size will do the job.

Faiz Farouk
  • 59
  • 1
  • 1
  • This works when retrieving a cursor of documents (such as from `db.myCollection.find()`), but not in this case of printing an array (such as from `db.myCollection.distinct("someValue")`). – M. Justin Jun 16 '23 at 16:13