meteor publish with limit and sort
Asked Answered
S

1

11

I have the following publication:

Meteor.publish('times', function() {
    return Times.find({}, {sort: {createdAt: -1}}, {limit: 5});
})

This returns all records, limit is ignored. However this

Meteor.publish('times', function() {
    return Times.find({}, {limit: 5});
})

returns 5 records, but in the wrong order. How do I limit and sort in a publication?

Sheritasherj answered 17/10, 2014 at 17:44 Comment(0)
B
21

See the example in the forEach section of the docs, and the documentation for find. limit is a key of the options object, so it should be:

Times.find({}, {sort: {createdAt: -1}, limit: 5});

Note that if you want the documents in sorted order on the client, you will need to sort them again in your template code.

Boykin answered 17/10, 2014 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.