jthomasbailey
jthomasbailey

Reputation: 410

sort Meteor collection according to a variable?

This works:

 return articles.find({}, {sort: {'published': -1}, limit: 1});

This doesn't:

sort_order = 'published';
return articles.find({}, {sort: {sort_order: -1}, limit: 1});

Is there some way to do this?

Upvotes: 1

Views: 1549

Answers (1)

Tarang
Tarang

Reputation: 75975

JSON can't use the first value in the key value pair as a variable when it is defined using the {} form of definition. You can however, use this way instead:

var sort_order = {};

sort_order["published"] = -1;

return articles.find({}, {sort: sort_order, limit: 1});

This should get you a final query of:

return articles.find({}, {sort: {published:-1}, limit: 1});

Upvotes: 6

Related Questions