【发布时间】:2015-01-10 23:02:18
【问题描述】:
我在看一些流星代码,我看到了这个:
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 数据库会执行两个查询吗?服务器部分?客户端部分? (然后是迷你 mongo)
是否可以在第二个函数中使用先前的结果?我试过了
notificationCount = function(){
this.notifications.length;
....
但它不起作用,但也许流星记得以前的结果并使用它? 我肯定会在我的模板中返回一个 something.find() 以拥有一个光标,然后返回其他变量,例如:count,或者用字段或其他东西过滤它,所以我对这个问题很感兴趣。
哪位高手给我解释一下?非常感谢流星社区:)!
【问题讨论】:
-
来自 Mongo 文档:Count 不执行查询,而是计算查询返回的结果。
标签: meteor