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.
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.
To filter MongoDB queries, proceed with the following steps:
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}}); }, ... });
Save
main.js
and start Meteor if necessary. Navigate tohttp://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...