Book Image

Meteor Cookbook

By : Isaac Strack
Book Image

Meteor Cookbook

By: Isaac Strack

Overview of this book

Table of Contents (19 chapters)
Meteor Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. 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}}
  2. Next, modify the Comments.insert action inside the click event handler, found in [project root]/client/script/main.js:

    Template.hello.events({
        'click button' : function () {
          // template data, if any, is available in 'this...