Publishing specific fields
To improve publications, we can also determine which fields we want to publish from the document. For example, we can only ask for the title
and text
properties instead of all other properties.
This speeds up the synchronization of our subscriptions since we don't require the whole post but only the necessary data and short descriptions when listing posts on the front page.
Let's add another publication to our publications.js
file:
Meteor.publish('specificfields-posts', function () { return Posts.find({}, { fields: { title: 1 } }); });
As this is just an example, we pass an empty object as a query to find all the documents, and as the second parameter to find()
, we pass an options object containing the fields
object.
Every field that we give a value of 1
will be included in the returned document. If we rather want to work by excluding fields, we can use the field name and set the value to 0
. However, we can't use both including and excluding fields...