我一直在尋找一些流星代碼,我看到了這一點:流星模板優化
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 then)
是否可以在第二個函數中使用先前的結果?我試過
notificationCount = function(){
this.notifications.length;
....
但它不起作用,但也許流星記得以前的結果並使用它? 我肯定會在我的模板中返回一個something.find()來獲得遊標,然後返回其他變量,例如:count,或者用字段或其他東西過濾它,所以我這個問題的結果。
任何專家解釋我?非常感謝流星社區:)!
從蒙戈文檔:[COUNT](http://docs.mongodb.org/manual/reference/method/cursor.count/#cursor.count)不執行查詢,而是對查詢返回的結果進行計數。 – solarc