订阅大集合

时间:2014-10-29 03:33:49

标签: javascript meteor publish-subscribe

我有一个最近变得非常大的集合。我曾经在myapp上使用搜索字段对其进行搜索时订阅了所有文档。

使用这个更大的集合,需要花费大量的时间来加载。

您建议什么方法可以降低加载时​​间,但仍可以搜索整个系列?

我可以通过客户订阅集合的一部分(执行我的搜索查询)吗?

我现在拥有的东西:

在我的路由器上:

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

在我的客户端上(点击搜索按钮时):

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

模板

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;
  }
})

2 个答案:

答案 0 :(得分:1)

解决方案是在服务器上定义publish function,将搜索关键字作为参数。

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: ...
    }
  ); 
});

然后在客户端上,将keywords作为参数传递:

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

还值得注意的是,可能有包装正在做你想要的东西,例如autocompletedatatables

另请参阅了解Meteor发布/订阅。

答案 1 :(得分:0)

您应尝试尽可能少地发布数据。

仅发布(并订阅)搜索中使用的集合的字段(请参阅http://docs.meteor.com/#find),并编写一个按ID返回整个File对象的方法,并在您之后调用它搜索(当您显示文件内容或其他内容时)。

其他选项,您可以在不发布Files集合的情况下执行搜索服务器端,但结果不会太多“meteor-ish”......

相关问题