meteor - 找出 Meteor.js 集合上监听的所有查询

标签 meteor

在 Meteor 0.7.0.1 中,是否可以计算/找出当前正在监听特定 Collection 的所有查询?

我正在尝试创建一个函数,该函数执行以下操作:每当监听特定查询的用户数量(例如:myCollection.find({color:'red'}) 变为非零时,每当文档被更改/添加到第二个集合anotherCollection时执行函数。

最佳答案

何时/如何调用 find 方法?例如,如果当有人点击页面上的按钮时调用它,只需增加一个服务器端变量,该变量在发生这种情况时就会增加。要在用户离开页面时减少此变量,请监听 window.onbeforeunload 事件并在发生该事件时减少计数。

或者,如果您有登录系统,请为每个用户分配一个 bool 值,例如 online。当他们登录时,使用以下代码使他们的在线状态正确。 if(Meteor.user()){Meteor.user().online=true;}。确保在他们离开时 onbeforeunload 将其在线状态设置为 false。然后,执行诸如 Meteor.users.find({onl​​ine:true}).size() 之类的操作来获取在线用户数量。

基本上,不要在调用 myCollection.find({color:'red'}) 时进行更新,而是将其放入函数中。例如:

if(Meteor.isClient){
Session.set('browsing', false);
function findColor(c){//Alerts the server when the user attempts to get a color. 
//This is presuming they don't use the plain MongoDB command. 
//Your button/however you're finding the color should use this command
if(!Session.get('browsing'))//if it's already true, don't increase the count
{
Meteor.call('incBrowsing');
Session.set('browsing', true);
}

return myCollection.find({color:c});
}
window.onbeforeunload = function(){Meteor.call('decBrowsing');};
}

if(Meteor.isServer){
var browsing = 0;
Meteor.methods({
incBrowsing: function(){browsing++;},
decBrowsing: function(){browsing++;}
});
}

我还没有测试过这个,但我希望它对你有用。您没有提供有关问题的太多详细信息,因此如果您或我需要澄清某些内容,请随时在下面发表评论。

关于meteor - 找出 Meteor.js 集合上监听的所有查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036802/

相关文章:

meteor - 在Atom Editor上开发 meteor 的最佳软件包是什么?

javascript - { [错误 : failed [405] HTTP method not allowed, 支持的方法:POST] 堆栈:[Getter] }

javascript - 应该在哪里设置 Meteor 默认值?

javascript - 为什么我的 Meteor API 调用抛出异常?

javascript - 在 Meteor 的测验应用程序中显示提示

meteor - 如何使用 CouchDB 作为 MeteorJS 应用程序的后端数据库,而不是默认的 MongoDB?

javascript - 将变量传递给 mongo 修饰符

javascript - Meteor - 服务器端 API 调用并每分钟插入 mongodb

javascript - 在我执行 git init 并创建一个新分支后,Meteor 崩溃了

javascript - 在 Meteor 中对发布、订阅和数据进行过滤和排序