0

Imagine you have a collection similar to the following...

Tests = [
  {
    name: 'Some Test',
    questions: [
      { question: 'Answer to life, the universe, and everything?', answer: '42' },
      { question: 'What is your favorite color?', answer: 'Blue' },
      { question: 'Airspeed velocity of unladen European Swallow?', answer: '24 mph' }
    ]
  }
]; 

How do you publish the entire collection except for the answer property?

I understand you can do the following to omit properties from the publish...

Meteor.publish('tests', function() {
  return Tests.find({}, {fields: {name:0}});
});

But I'm not sure how to omit a property from an array property.

Thanks!

mackysassr
  • 171
  • 1
  • 4
  • It must be something like `return Tests.find({}, {fields: {'questions.$.answer':0}});` But it seems to be unsupported yet – igor Mar 17 '15 at 19:31

1 Answers1

0

It can't be done the way you want to do it. Meteor only supports field specifiers that are 1 level deep. You can sometimes get a sub-field specifier to work, but it's not reliable.

You can put your questions into their own collection with a testId field that links them back to the test, relational style. One question per document, and then you'll be able to specify that only the question field gets published.

Meteor.publish ('questions', function(testId) {
    return Questions.find({testId: testId}, {fields: {question: 1}})
});

It's not ideal, but pretty painless compared to trying to find a workaround that allows your questions to live in the test document.

There might be a way to do this manually with a more involved publish. There's a similar question here with an answer that gets into it.

Community
  • 1
  • 1
RevMen
  • 562
  • 3
  • 14