onjegolders
onjegolders

Reputation: 1059

How to perform a find and sort on a Meteor collection (using Coffeescript)

I'm new to Meteor and struggling to find any examples of using find and sort on a collection (and specifically using CS).

I seem to have a basic find operation working:

Tasks.find(done: false)

but am struggling to see how to add on a sort query to this? Are there any good examples of these queries? (Even in pure JS)? Many thanks.

Upvotes: 1

Views: 792

Answers (2)

David Weldon
David Weldon

Reputation: 64312

You can see the documentation for find here. If you just search the docs for sort: you'll find several examples. Using your code above, you could sort your completed tasks by completedAt like so:

Tasks.find {done: true}, sort: completedAt: -1

Upvotes: 1

Michel Floyd
Michel Floyd

Reputation: 20227

A brief primer-by-example in JS:

Tasks.find({done: false}, {order: {status: 1}});

would sort by status ascending.

Tasks.find({done: false},{order: {status: 1, assignee: -1}});

will sort by status ascending then assignee descending.

Tasks.find({done: false},{order: {status: 1}, limit: 5});

will limit the results to the first 5.

For CS, you could define the options object itself as:

options = 
  sort:
    status: 1

and then do

Tasks.find(done: false, options)

Upvotes: 2

Related Questions