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

前端 未结 4 739
栀梦
栀梦 2021-01-05 05:10

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: {$nat

4条回答
  •  有刺的猬
    2021-01-05 05:30

    Sort isn't a parameter but a separate function to be called after find() on the resulting Cursor object. This is the method that the MongoDB documentation is referring to and works with drivers such as MongoJS:

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

    It seems Meteor hasn't added the sort() function to their implementation of Cursor, so an alternate solution would be to sort by the _id field which is generated based on date (and hence insertion order):

    return Answers.find({}, {sort: {'_id': -1}});
    

提交回复
热议问题