meteormeteor-collections

Finding documents in meteor.js


I'd like if someon could clarify this point about subscriptions in Meteor.js:

as written in the docs the find() function returns a cursor, not the data, that needs to be fetched; let's say I have a publish function:

Meteor.publish('pages', function() {
    return Pages.find()
});

now let's say I need only the page with code: "one"; in a template helper I can fetch that document:

Template.mytemplate.helpers({
    data: function() {
        return Pages.find({code: "one"});
    }
});

The question is: is correct to do this, or if I need only one document would be better to subscribe only that document? (something like:

Meteor.publish('page', function(code) {
    return Pages.find({code: code})
});

)

I mean: does it changes something in terms of efficiency/performance?


Solution

  • Yes, it does change a lot. If you subscribe to the entire collection, then the entire collection will be send to the client and kept in sync with the server. So by all means, if you only need one document, and do not need to cache any of the other documents (e.g., for switching to a new document), then only subscribe to the one you need.