[SOLVED] Meteor 1.5 and Publish filtering

I’m having some trouble with Publish and filtering results, so maybe I’m misunderstanding, but I don’t think so.

I have a template, that I want to publish a limited set of results to, and only a limited number of fields in those results.

My Publish function looks like this:

Meteor.publish("giveAGiftSet", function() {
    return Recipients.find({ webRecipient: true }, { "name.first": 1, gender: 1, gifts: 1, webRecipient: 1 });
});

When I run the template, only the limited number of results are returned based on the webRecipients: true portion, but when I use MeteorToy to look at the actual subscription data, I can see all of the fields, not just those I’ve asked for in the publish query.

Am I misunderstanding something, or have I done something incorrectly?

When I run that same query in Meteor Mongo, I get only the fields I’m requesting back.

Any help, advice, and / or guidance is greatly appreciated.

I think you need:

return Recipients.find({ 
  webRecipient: true
}, { 
  fields: { 
    'name.first': 1, 
    gender: 1, 
    gifts: 1, 
    webRecipient: 1 
  }, 
});

In Meteor, things like field, sort and limit are passed in as keys of the second object in the find function.

2 Likes

As always, you guys are awesome. Can’t believe that 1. I missed that, and 2. the system didn’t barf while running the query without it.

For any newbies who may not be able to see the difference, @vigorwebsolutions had me add the ‘fields:’ portion to my query indicating only wanted those fields back.

Thanks so much.

1 Like