picsoung
picsoung

Reputation: 6538

Subscription on large collection

I have a collection that recently became very large. I used to subscribe to all it's documents has I was performing search on it using a search field in myapp.

With this larger collection it takes a lot of time to load.

What would you recommend to lower the load time but still power search on the whole collection?

Can I subscribe to a part of the collection (performing my search query) through the client?

what I have now:

on my router:

waitOn: function(){
  return [Meteor.subscribe('files')];
},

on my client (when search button clicked):

'click #search':function(e,context) {
  Session.set("keywords",$("#search_input").val());
}

the template

Template.filwsList.helpers({
  files_results: function () {
    var keywords = new RegExp(Session.get("keywords"), "i");
    var result = Files.find({$or:[{name:keywords},{description:keywords},{tags:keywords}]},{sort: {updatedAt: 1}});
    return result;
  }
})

Upvotes: 0

Views: 76

Answers (2)

Dan Dascalescu
Dan Dascalescu

Reputation: 151855

The solution is to define a publish function on the server that takes as a parameter the search keyword.

Meteor.publish('files', function publishFunction(keywords) {
  check(keywords, String);  // https://docs.meteor.com/#/full/check_package
  return Files.find({
    $or: [
      { name: keywords },
      { description: keywords },
      { tags: keywords }
    ]}, {
      sort: ...
      limit: ...
      fields: ...
    }
  ); 
});

Then on the client, pass the keywords as a parameter:

waitOn: function () {
  return Meteor.subscribe('files', keywords);
}

It's also worth noting that there might be packages doing what you want already, e.g. autocomplete or datatables.

See also Understanding Meteor publish/subscribe.

Upvotes: 1

alexdown
alexdown

Reputation: 262

You should try to publish as little data as possible.

Publish (and subscribe to) only the field of the collection that are used in the search (see http://docs.meteor.com/#find ) and write a method that returns the whole File object by id and call it after your search (when you are displaying the file content or something).

Other option, you could just do the search server-side, without publishing the Files collection, but the result will not be much "meteor-ish"...

Upvotes: 0

Related Questions