[DOCS-7183] Comment on: "manual/reference/method.txt" Created: 17/Feb/16  Updated: 03/Nov/17  Resolved: 04/Oct/16

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Bug Priority: Major - P3
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu, MongoDB 3.0.9 version , Using RoboMongo

Location: https://docs.mongodb.org/manual/reference/method/
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0
Referrer: https://docs.mongodb.org/manual/reference/method/setVerboseShell/
Screen Resolution: 1440 x 900
repo: docs
source: reference/method


Participants:
Days since reply: 7 years, 51 weeks, 6 days ago

 Description   

I have a collection such as:-

{ "_id" : ObjectId("56c2a7320efba7c30cdb139b"), "country_code" : "US", "country_name" : "United States", "region_name" : "Hawaii", "city_name" : "'Aiea", "time_zone" : "-10:00\"\r" }

Am doing, db.collection.distinct("countries_name") to get the distinct country_names. Now i want to sort them , but sort is not applicable on distinct.
Error :-TypeError: db.countries_cities.distinct("country_name").sort(

{country_name:1}

) is not a function (shell):1



 Comments   
Comment by Ravind Kumar (Inactive) [ 18/Feb/16 ]

Draft reply:

Hello Ratnadeep,

The distinct command does not accept a sort parameter, nor does the shell's distinct() helper allow modifiers like .sort() to be chained.

However, since distinct() returns an array, once you get the results of distinct(), you could use JavaScript array's sort() method to sort the results on the client side. For example,

var result = db.foo.distinct("bar").sort();

Generated at Thu Feb 08 07:53:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.