0

I have a method as shown below on server.js file in meteor

  getNames : function(){
       var name = AccNames.find({}, {fields: {'Name': 1, '_id': 1}}).fetch();
        return name;
  }

How Can I sort the result in ascending order? I mean how can I get the list of NAME in ascending order? Is it possible to use fetch and sort together?

3
  • Possible duplicate of stackoverflow.com/questions/13957691/…. Commented Feb 15, 2016 at 11:45
  • How is it different? AccNames.find({}, {fields: {'Name': 1, '_id': 1},{sort: {'Name': 1}}).fetch(); didn't work? Commented Feb 15, 2016 at 11:54
  • No its not working.i don't know whats wrong! Commented Feb 15, 2016 at 11:56

1 Answer 1

1

Use any one of the sort specifier syntaxes as another property in the find() second argument object:

getNames: function(){
    // All of these do the same thing (sort in ascending order by key "name"

    /* 
       var sort_fields = [["Name", "asc"]];
       also the same as var sort_fields = ["Name"];
    */
    var sort_fields = {'Name': 1};
    var projection = {'Name': 1, '_id': 1};
    return AccNames.find({}, {fields: projection, sort: sort_fields}).fetch();    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Prefect Thanks a lot!

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.