做1:很多:许多人加入mongodb

问题描述:

感谢大家一直支持让这个难题的第一步特别是@GaëtanRouziès和@Neil解决。在此thread上,代码完美适用于有问题的场景。我想调整相同的代码来处理另一个类似的场景,但我还没有弄清楚可能会出现什么问题。这一次,我有一个coursemodule集合,这是课程和模块之间的许多关系集合,它们只存储coursesIdmoduleId。由于代码非常完美,我简单地复制,做了一点修改,并得出了下面的代码:做1:很多:许多人加入mongodb

courses(){ 
    var theslug = FlowRouter.getParam('myslug'); 
    var mySchoolDocs = SchoolDb.findOne({slug: theslug}); 
    var arrayModuleSchools = ModuleSchool.find({schoolId: mySchoolDocs._id}); 
    // Transform the array of document into an array with only the ids 
    var arrayModuleId = []; 
    arrayModuleSchools.forEach(function(moduleSchools){ 
     arrayModuleId.push(moduleSchools.moduleId); 
    }); 
    var coursetoMod = CourseModules.find({}, {moduleId: {$in: arrayModuleId}}); 
    if (coursetoMod) { 
     coursesArrayIds = []; 
     console.log(coursetoSchool); 
     coursetoMod.forEach(function (courseToModules) { 
      coursesArrayIds.push(courseToModules.coursesId); 
     }); 
     return Courses.find({_id: {$in: coursesArrayIds}}).fetch(); 
    } 
} 

具体而言,在Modules集合中只存在2个模块,与IDS - xfLM9DEzhCMYQpQ32 and PTbZQ9cTG9pByFsY2。该CourseModule收藏有这有文档:

{ 
    "_id" : "iXX4unJZRNcCw9bAm", 
    "moduleId" : "PTbZQ9cTG9pByFsY2", 
    "coursesId" : "FbgcdZxADHKRBj98z", 
    "createdAt" : ISODate("2017-08-25T16:36:17.173Z"), 
    "userId" : "n5rqFSHbhm7zqADyB" 
} 
{ 
    "_id" : "RAJJFjqAjGoDeNhko", 
    "moduleId" : "PTbZQ9cTG9pByFsY2", 
    "coursesId" : "ESAf6NGpZzXeioecp", 
    "createdAt" : ISODate("2017-08-25T16:36:17.182Z"), 
    "userId" : "n5rqFSHbhm7zqADyB" 
} 
{ 
    "_id" : "8ceuFwZK8Qduo5J5P", 
    "moduleId" : "xfLM9DEzhCMYQpQ32", 
    "coursesId" : "KnNj4GLcyMtvF8JmB", 
    "createdAt" : ISODate("2017-08-25T16:38:15.368Z"), 
    "userId" : "n5rqFSHbhm7zqADyB" 
} 

点在哪里我登录到我的selectorId未定义控制台:

1 ... n.Cursor {集合:LocalCollection,分拣机:空,匹配: 中号... o.Matcher,_selectorId:未定义,跳过:未定义...} _projectionFn: (OBJ)_selectorId:undefined_transform:nullcollection: LocalCollectionfields:undefinedlimit:undefinedmatcher: Minimongo.Matcherreactive:trueskip:undefinedsorter:null__proto__: Object_depend:(changers,_allow_unordered)_getCollectionName: ()_getRawObjects :(选项)_publishCursor:(子)构造函数: count :()fetch :()forEach :(callback, thisArg)getTransform :()map:(callback,thisArg)观察: (options)observeChanges:(options)rewind :()proto:Object view.js:30 L ... n.Cursor {collection:LocalCollection,sorter:null, Matcher:M ... o.Matcher,_selectorId:undefined,skip:undefined ...}

我想要做的就是获取当前通过模块显示的特定学校附加的课程。

+0

哪个控制台产生的错误信息?铬调试器控制台?或者“流星蒙哥”控制台?或者'流星'服务器控制台? –

您正在使用find功能走错了路:

var coursetoMod = CourseModules.find({}, {moduleId: {$in: arrayModuleId}}); 

find()函数有两个参数:myCollection.find(query, projection)。当按字段过滤文档时,它必须在query参数内。而projection参数用于选择要返回的字段。

对于您的情况,以下是您正在使用的参数:query: {}projection: {moduleId: {$in: arrayModuleId}}。但它需要:query: {moduleId: {$in: arrayModuleId}}

所以,你只需要使用$in作为第一个参数

var coursetoMod = CourseModules.find({moduleId: {$in: arrayModuleId}}); 

顺便说一句,如果你想直接看到里面的find函数返回的文件一个console.log,使用.fetch()

var coursetoMod = CourseModules.find({moduleId: {$in: arrayModuleId}}).fetch(); 

MongoDB中找到函数文档:https://docs.mongodb.com/manual/reference/method/db.collection.find/

+0

仍然得到控制台上的同样的信息:'''1 ... n.Cursor {集合:LocalCollection,分拣机:空,匹配:M ... o.Matcher,_selectorId:未定义,跳过:未定义...}''' – kehinde

+0

当我添加.fetch到查询它返回一个空数组。 '''[]''' – kehinde

+1

第一条消息是正常的,导致find()返回一个Cursor对象。使用fetch(),我们知道它返回0文档。你需要检查更多的数据:'console.log(coursetoMod);的console.log(arrayModuleId);的console.log(CourseModules.find({})取()); console.log(coursesArrayIds);'在调试时,如果您不确定,请检查更多数据。 –

@gaetan是在正确的轨道的答案,你需要使用的查询参数,而不是投影参数。在代码中还可以使用与Meteor一起打包的underscore库进行一些其他简化。

courses() { 
    const slug = FlowRouter.getParam('myslug'); 
    const schoolId = SchoolDb.findOne({ slug })._id; 
    const Modules = ModuleSchool.find({ schoolId }); 
    const ModuleIds = _.pluck(Modules,'moduleId'); 
    const coursetoMod = CourseModules.find({ moduleId: { $in: ModuleIds }}); 

    if (coursetoMod.length) { 
    coursesIds = _.pluck(coursetoMod,'coursesId'); 
    return Courses.find({ _id: { $in: coursesArrayIds }}).fetch(); 
    } 
}