几个月前我问过一个问题,Meteor似乎有答案。
Which, if any, of the NoSQL databases can provide stream of *changes* to a query result set?
Meteor如何接收MongoDB查询结果的更新?
谢谢,
克里斯。
发布于 2012-04-12 02:32:40
你想让query.observe()
来做这个。假设您有一个带有标签字段的Posts
集合,并且您希望在添加带有important
标签的帖子时得到通知。
http://docs.meteor.com/#observe
// collection of posts that includes array of tags
var Posts = new Meteor.Collection('posts');
// DB cursor to find all posts with 'important' in the tags array.
var cursor = Posts.find({tags: 'important'});
// watch the cursor for changes
var handle = cursor.observe({
added: function (post) { ... }, // run when post is added
changed: function (post) { ... } // run when post is changed
removed: function (post) { ... } // run when post is removed
});
如果您想在post更改时在每个浏览器中执行某些操作,则可以在客户机上运行此代码。或者你可以在服务器上运行,如果你想在添加重要帖子时向团队发送一封电子邮件。
请注意,added
和removed
指的是查询,而不是文档。如果您有一个现有的post文档并运行
Posts.update(my_post_id, {$addToSet: {tags: 'important'}});
这将触发“已添加”回调,因为帖子将被添加到查询结果中。
发布于 2013-03-01 17:10:41
目前,Meteor在一个实例/进程上工作得很好。在这种情况下,所有查询都要经过这个实例,它可以将其广播回其他客户端。此外,它每10秒轮询一次MongoDB,以查看外部查询对数据库的更改。他们计划在1.0版本中提高可伸缩性,并希望允许多个实例通知每个实例有关更改的信息。
另一方面,DerbyJS正在使用Redis PubSub。
https://stackoverflow.com/questions/10103541
复制相似问题