Mongodb 如何阅读流星中依赖于另一个收藏的收藏
我试图从一个集合中加载最新的帖子,同时加载同一帖子的所有评论。集合具有引用,而不是将整个文档存储在彼此内部:Mongodb 如何阅读流星中依赖于另一个收藏的收藏,mongodb,meteor,Mongodb,Meteor,我试图从一个集合中加载最新的帖子,同时加载同一帖子的所有评论。集合具有引用,而不是将整个文档存储在彼此内部: Post { title, body, etc..} Comment { postId, body, etc.. } 我使用iron router作为路由包,在我页面的路由中,我使用以下方式订阅: this.route('home', { path: '/', template: 'home', waitOn: function () { ret
Post { title, body, etc..}
Comment { postId, body, etc.. }
我使用iron router作为路由包,在我页面的路由中,我使用以下方式订阅:
this.route('home', {
path: '/',
template: 'home',
waitOn: function () {
return [
Meteor.subscribe('latestPost'),
Meteor.subscribe('lastReadPost')
];
}
});
检索帖子的代码如下所示:
Posts.findOne({}, {sort:{createdAt:-1, limit:1}});
现在的问题是,我不知道如何在不阅读整个集合的情况下检索注释。我无法在路由器中订阅,因为我仍然没有查询评论集合的帖子ID。
我猜我可以从模板中完成这项工作,但当然,如果我查询Comments集合,它仍然是空的。但我确实有post,因为当时它在Posts集合中。但我需要从模板触发订阅,这听起来不像是一个干净的解决方案
最佳实践是什么?谢谢 服务器端代码:
Meteor.publish("latestPost", function () {
var post = Posts.find({}, {sort:{created:-1}}).fetch()[0];
console.log("publish : " + post.title);
return [
Posts.find({_id: post._id}),
Comments.find({postId: post._id})
];
});
this.route('home', {
path: '/',
template: 'home',
waitOn: function () {
return [
Meteor.subscribe('latestPost')
];
},
data:function(){
return {
post:Posts.findOne(),
comments:Comments.find()
};
}
});
客户端代码:
Meteor.publish("latestPost", function () {
var post = Posts.find({}, {sort:{created:-1}}).fetch()[0];
console.log("publish : " + post.title);
return [
Posts.find({_id: post._id}),
Comments.find({postId: post._id})
];
});
this.route('home', {
path: '/',
template: 'home',
waitOn: function () {
return [
Meteor.subscribe('latestPost')
];
},
data:function(){
return {
post:Posts.findOne(),
comments:Comments.find()
};
}
});
选中此项查看整个示例
用户更改另一条路线后,子描述将自动停止 我还将在服务器端查找器选项中包含一个限制
{sort:{created:-1},limit:1}这正是我所需要的。非常感谢!