Javascript Mongo:获取两个集合的数据-秒取决于第一个集合
我试图显示一篇文章,在这篇文章的下面我想显示一些属于这篇文章的文献数据。 在收藏文献中有很多文档,但是我想过滤那些有Javascript Mongo:获取两个集合的数据-秒取决于第一个集合,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我试图显示一篇文章,在这篇文章的下面我想显示一些属于这篇文章的文献数据。 在收藏文献中有很多文档,但是我想过滤那些有文章\u id:article.\u id 我认为我的想法很复杂,但这就是我想做的: publication.js Meteor.publish('references', function(){ return Articles.find({}); }); Router.route('/cars', { name: 'main', data: functi
文章\u id:article.\u id
我认为我的想法很复杂,但这就是我想做的:
publication.js
Meteor.publish('references', function(){
return Articles.find({});
});
Router.route('/cars', {
name: 'main',
data: function() {
return {
article: Articles.find({})
}
}
});
Template.literature.helpers({
items: function() {
return Literature.find({article_id: article._id}); /* this should be the id the first template */
}
});
Router.js
Meteor.publish('references', function(){
return Articles.find({});
});
Router.route('/cars', {
name: 'main',
data: function() {
return {
article: Articles.find({})
}
}
});
Template.literature.helpers({
items: function() {
return Literature.find({article_id: article._id}); /* this should be the id the first template */
}
});
模板_1.html
<template name="main">
<div>
{{article._id}}
<header><h1>{{article.title}}</h1></header>
{{article.content}}
{{>literature reference=article._id}}
</div>
</template>
<template name="literature">
Same id: {{this.reference}}
Now get all item of literature-collection
<ul>
{{#each items}}
<li>{{this.title}}</li>
{{/each}}
</ul>
</template>
我想这可能更容易。因此,我的第二个想法是将文献查询也放入路由器:
Router.route('/cars', {
name: 'main',
data: function() {
return {
article: Articles.find({}),
items: Literature.find({article_id: article._id}) /* How do I get the variable which is needed? */
}
}
});
您应该使用meteor add reywood:publish composite 因此,您的发布功能将如下所示:
Meteor.publishComposite('articles', function(){
return {
find: function(){
return Articles.find();
},
children: [{
find: function(article){
return Literature.find({article_id: article._id})
}
}]
}
});
我不认为这是一个流星问题,但这是一个Mongo问题——这更像是一个正确建模数据的问题,而不是查询数据的问题——例如,为什么您有两个集合而没有一个?在任何情况下,从--“我如何使用mongshell构造查询”…那么半隐藏的注释是您的问题吗?谢谢。我如何在模板中使用它?现在,我在路由器中获取数据,并将其放入一个变量中。您在iron:router中订阅waitOn函数,然后像在示例中一样使用模板,所有有关文章的文献都将在客户端上