Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.js: Find all documents and return in reverse natural order

I am trying to return all documents in a collection, to use it with an {{#each}} in my template. My code looks like this:

return Answers.find({}, {sort: {$natural:-1}})

But the documents are returned in natural order (not reverse). Does anyone know why? I got the $natural selector from the MongoDB documentation, so I don't see what's wrong.

like image 735
Erlend V Avatar asked Nov 25 '12 18:11

Erlend V


1 Answers

Can't tell why don't it returns in reverse order.

But you can create an array in the template helper method and return reverse of an array using array.sort() or array.reverse() functions.

For ex: Say you Answers collection looks like this:

Answers({ansNo: 1, ansBody: "body1"},
        {ansNo: 2, ansBody: "body2"},
        {ansNo: 3, ansBody: "body3"});

And the array to be returned is:

var AnswersArr = new Array();

then in your template helper :->

var tempCollection = Answers.find({});
tempCollection.forEach(function(data){
    var obj = {ansNo: data.asnNo, ansBody: data.ansBody};
    AnswersArr.push(abj);
});

AnswersArr.sort(function(a, b){return b.ansNo - a.ansNo;});  //sort in reverse order

return AnswersArr;
like image 164
sohel khalifa Avatar answered Nov 16 '22 01:11

sohel khalifa