使用Array使用Meteor js在集合中查找文档 [英] Using array to find documents in a collection using Meteor js
问题描述
帮助程序方法
GetSchoolId:function(){
var moduleIdArray = [];
var myslug = FlowRouter.getParam('myslug');
var mySchoolId = SchoolDb.findOne({slug:myslug});
if(mySchoolId){
ModuleSchool.find({},{schoolId:mySchoolId._id}).forEach(function(modulesSelected){
moduleIdArray.push(modulesSelected.moduleId);
});
if(typeof moduleIdArray ==='object'&& moduleIdArray instanceof Array){
console.log(moduleIdArray);
moduleIdArray.forEach(function(moduleIds){
return Modules.find({},{_id:moduleIds})。fetch();
});
$ b
模板代码:
{{#each GetSchoolId}}
< p> {{GetSchoolId.modulename}}< / p>
{{/ each}}
< / p>
{{/每个}}
我知道Meteor Profs,它是一寸一个冰山,几秒钟内就会被丢弃。我有3个集合,一个是学校记录( SchoolDb
),第二个是模块(Modules),第三个是关系表( ModuleSchool
)。模块分配给学校。
从上面的代码中,我可以从( SchoolDb
)使用传递给我用来从关系表( SchoolDb
)中获取 schoolId
( ModuleSchool
)返回分配给相关学校的模块。我能够获取模块ID并将它们转换为数组,我现在要做的就是使用从 ModuleSchool
中获取的ID数组返回模块名称因为只有 _ids
存储在关系表中。
上面的代码只能达到级别将_ids转换为数组,当我试图在模板上打印什么都没有显示。我有什么不对的?
要找到一个字段对应至少一个数组的元素,您可以使用 $ in
:
Template.my_template.helpers({
模块(){
var myslug = FlowRouter.getParam('myslug');
var mySchoolDoc = SchoolDb.findOne({slug:myslug});
var arrayModuleSchool = ModuleSchool.find {schoolId:mySchoolDoc._id});
//将文档数组转换为仅包含ids的数组
var arrayModuleIds = [];
arrayModuleSchool.forEach(function(moduleSchool)) {
arrayModuleIds.push(moduleSchool.moduleId);
});
return Modules.find({_ id:{$ in:arrayModuleIds}}。fetch();
}
});
然后简单地使用 {{each}}
标签。
< template name =my_template>
{{#each module in modules}}
< p> {{module.modulename}}< / p>
{{/ each}}
< / template>但是,如果每个模块只连接到一所学校,我建议你的问题更简单的解决方案,你不必在学校和模块之间创建一个集合。
您只需要创建两个集合: Schools
和 Modules
并添加到模块文档a schoolId
字段。
然后你的代码看起来像这样:
Template.my_template.helpers({ b $ b modules(){
var myslug = FlowRouter.getParam('myslug');
var mySchoolDoc = Schools.findOne({slug:myslug});
返回Modules.find {schoolId:mySchoolDoc._id})。fetch();
}
});
Helper method
GetSchoolId: function () {
var moduleIdArray=[];
var myslug = FlowRouter.getParam('myslug');
var mySchoolId = SchoolDb.findOne({slug: myslug});
if (mySchoolId) {
ModuleSchool.find({}, {schoolId: mySchoolId._id}).forEach(function (modulesSelected) {
moduleIdArray.push(modulesSelected.moduleId);
});
if (typeof moduleIdArray === 'object' && moduleIdArray instanceof Array) {
console.log(moduleIdArray);
moduleIdArray.forEach(function (moduleIds) {
return Modules.find({}, {_id: moduleIds}).fetch();
});
}
}
}
Template code:
{{#each GetSchoolId }}
<p>{{GetSchoolId.modulename}} </p>
{{/each}}
</p>
{{/each}}
I know to Meteor Profs, it is a inch of an iceberg, in seconds it will be trashed. I have 3 collections, one for school record (SchoolDb
), second for for module (Modules), and the third, a relationship table (ModuleSchool
). modules are assigned to schools.
From the code above, I am able to get the school _id from (SchoolDb
) using the slug passed to the route which I used to fetch the schoolId
from the relationship table (SchoolDb
) and the (ModuleSchool
) to return modules assigned to a the school in question. I was able to fetch the module Ids and converted them into arrays, what I now want to do is using the array of Ids fetched from the ModuleSchool
to return the module names from the Modules because only the _ids
are stored in the relationship table.
The above code does only does it to the level of converting the _ids to array, when I tried printing on the template nothing showed. What wrong am I to right?
解决方案 To find document with a field corresponding to at least one element of an array you can use $in
:
Template.my_template.helpers({
modules(){
var myslug = FlowRouter.getParam('myslug');
var mySchoolDoc = SchoolDb.findOne({slug: myslug});
var arrayModuleSchool = ModuleSchool.find({schoolId: mySchoolDoc._id});
// Transform the array of document into an array with only the ids
var arrayModuleIds = [];
arrayModuleSchool.forEach(function(moduleSchool)){
arrayModuleIds.push(moduleSchool.moduleId);
});
return Modules.find({_id: {$in: arrayModuleIds}}).fetch();
}
});
And then simply use the {{each}}
tag.
<template name="my_template">
{{#each module in modules}}
<p>{{module.modulename}}</p>
{{/each}}
</template>
But if each module is attached to only one school, i suggest a simpler solution for your problem, you don't have to create a collection between school and module.
You just have to create two collections: Schools
and Modules
and add to the Modules documents a schoolId
field.
Then your code would look like that :
Template.my_template.helpers({
modules(){
var myslug = FlowRouter.getParam('myslug');
var mySchoolDoc = Schools.findOne({slug: myslug});
return Modules.find({schoolId: mySchoolDoc._id}).fetch();
}
});
这篇关于使用Array使用Meteor js在集合中查找文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!