Trouble separating out publication results

Hi, I’ve got a query that uses a mongo $text operator which can’t be executed on the client (no support in minimongo). So I’m doing it in my publication. However the same collection is publishing a different set of documents for a different query that appears on a different part of the same page. On the client, where I want to show the results of my $text query, it is also showing the records from the different query.

Is there a way on the client to restrict the results of a .find() to only those published by a nominated subscription?

yeah…

refer to one of the answer on … http://stackoverflow.com/questions/12223866/meteor-publish-subscribe-strategies-for-unique-client-side-collections

then you can do something like :-

Collections.Products = new Meteor.Collection("products");
Collections.ProductSearch = new Meteor.Collection("productsearch"); // fake collection to separate out product search 

then

  Meteor.publish("productSearch", function(search) {
    var query = {}
    if(search) {
      query["$text"] = {$search: search};
    }
    Mongo.Collection._publishCursor( Collections.Products.find(query), this, 'productsearch');
    this.ready();
  });
1 Like

Hi,

Sometimes switch is also useful: https://medium.com/modern-user-interfaces/how-we-redux-part-3-domain-890964824fec#.q750dxr76

Hat tip @abhiaiyer

// in server/publications.js

Meteor.publish(‘getTodos’, function (filter, pageSkip = 0) {
switch (filter) {
case ‘SHOW_ALL’:
return Todos.find({}, {skip: pageSkip, limit 10});
case ‘SHOW_COMPLETED’:
return Todos.find({completed: true}, {skip: pageSkip, limit: 10});
case ‘SHOW_ACTIVE’:
return Todos.find({completed: false}, {skip: pageSkip, limit: 10});
default:
return Todos.find({}, {skip: pageSkip, limit: 10});
}
});

Thanks so much,
tat

That seems workable if not terribly elegant.

The duality of having sometimes identical find statements on the client as in the publish method has never sat well with me. I think it would be good to be able to do something like subscription.find() which only returns a subset of the subscription (or everthing that the specific subscription published if no criteria are nominated). But I guess it’s all going to be moot when Apollo is ready.

Thanks for the reply and links.

Thanks for the reply, but that’s not really addressing the problem I have. I already have multiple publications on the one collection (equivalent to your switch). By the time they reach the client, it’s just all amalgamated and you have to separate out the records of interest.