Publish selected documents from a collection

Hi everyone,

I have a collection in my MongoDB database with 60,000 documents. However, I don’t need to return them all into my application.

35
Here’s an example of the data

I want to be able to narrow down the results based on if the Category field contains either the word CONV or CUMWR. What I have at the moment looks like this:

Meteor.publish('statistics', function publishSomeData() {
  return statistics.find({ "Category" : /.*CONV.*/ });
  return statistics.find({ "Category" : /.*CUMWR.*/ });
});

In my application however, this only shows me the data containing CONV and not CUMWR.

I also tried doing this:

Meteor.publish('statistics', function publishSomeData() {
  return statistics.find({ "Category" : /.*CONV.*/ }, { "Category" : /.*CUMWR.*/ });
});

But again the same issue occurs.

Can any body let me know what i’m doing wrong here?

Many thanks,
George

NOW SOLVED

Installed the smart publish package and now works like this:

Meteor.smartPublish('statistics', function publishSomeData() {
  return [
    statistics.find({ "Category" : /.*CONV.*/ }),
    statistics.find({ "Category" : /.*CUMWR.*/ })
  ]
});

Using $or should also work:

Meteor.publish('statistics', function publishSomeData() {
  return statistics.find({$or:[{Category : /CONV/ }, {Category : /CUMWR/ }]});
});
3 Likes

Be aware that although MongoDB tries to optimise regular expression searches on indexed fields, performance is likely to be worse than using non-regex searches.

https://docs.mongodb.com/manual/reference/operator/query/regex/#index-use

2 Likes

You know yo can send a parameter to the publish function through the subscibe call?
Meteor.subscribe("docsByCategory", "CONV")


Meteor.publish('docsByCategory', function (cat) {
  return statistics.find({Category : cat });
});