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

Filtering with MongoDB queries


Whether you're performing searches, organizing records, or narrowing down results, sooner or later, you'll want to filter the results of your collections. This recipe shows you how to limit the number of records in a collection, using MongoDB's find method options.

Getting ready

We will use the project created in the Sorting with MongoDB queries recipe, found in this chapter. Please complete that recipe, and use the files as a baseline for this recipe.

How to do it…

To filter MongoDB queries, proceed with the following steps:

  1. Make the following change to the comments helper function in [project root]/client/scripts/main.js:

    Template.hello.helpers({
      ...
      comments: function () {
        return Comments.find({number:/[2468]/},{sort:{number:-1}});
      },
      ...
    });
  2. Save main.js and start Meteor if necessary. Navigate to http://localhost:3000/; click the button on the screen several times and watch as only the comments that contain an even number are displayed. Your results...