Sorting with MongoDB queries
There are many times when you will need to sort a collection. Most recent comments, alphabetized lists, and bank transactions by amount are all good examples. This recipe will show you how to sort MongoDB collections, using the options in your find()
request.
Getting ready
We will be using the Implementing a simple collection recipe found in this chapter as a baseline. please follow that recipe in preparation for this activity.
How to do it…
To perform sorting with MongoDB queries, proceed with the following steps:
- In your
main.html
file (located at[project root]/client/main.html
), make the following modification to the{{#each…}}
template fragment:{{#each comments}} <div>{{text}}{{number}}</div> {{/each}} {{greeting}}
- Next, modify the
Comments.insert
action inside theclick
event handler, found in[project root]/client/script/main.js
:Template.hello.events({ 'click button' : function () { // template data, if any...