Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 流星模板优化_Meteor - Fatal编程技术网

Meteor 流星模板优化

Meteor 流星模板优化,meteor,Meteor,我在看流星密码,我看到了: Template.notifications.helpers({ notifications: function() { return Notifications.find({userId: Meteor.userId(), read: false}); }, notificationCount: function(){ return Notifications.find({userId: Meteor.userId(), read: fa

我在看流星密码,我看到了:

Template.notifications.helpers({
  notifications: function() {
    return Notifications.find({userId: Meteor.userId(), read: false});
  },
  notificationCount: function(){
    return Notifications.find({userId: Meteor.userId(), read: false}).count();
  }
});
所以我想知道,这是优化的吗? 我的意思是,mongo数据库会执行两个查询吗?服务器部分?客户部分?(然后是mini mongo)

是否可以在第二个函数中使用上一个结果?我试过了

notificationCount = function(){
    this.notifications.length; 
....
但它不起作用,但也许流星记得以前的结果并使用它? 在我的模板中,我肯定会返回一个something.find()以获得一个游标,然后返回其他变量,例如:count,或者使用字段或其他内容对其进行过滤,所以我被这个问题所困扰

有专家给我解释吗?非常感谢流星社区:)

  • 对。在客户端,而不是服务器端
  • 您可以在模板上使用{{notifications.count}}

  • 您没有在服务器上执行2个查询,这是最关键的位置。订阅数据时,数据将进入浏览器中名为MiniMongo的本地数据库。您可以在客户机上运行任意数量的查询,数据集(通常,我们希望如此)很小,并且没有明显的性能损失

    如果您有一些性能问题,您可以将
    通知的结果保存到Session或另一个反应式字典中。find({userId:Meteor.userId(),read:false})
    。这将略微提高性能,因为您节省了Minimongo的查询时间:解析、搜索等

    在服务器上,您应该尽可能小心。服务器中的瓶颈可能意味着您的整个应用程序的速度不如预期的快


    阅读有关mini-in-memory数据库的更多信息:

    来自Mongo文档:不执行查询,而是统计查询将返回的结果。