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

问题描述 投票:0回答:2

在这个earlier thread上,我的一些代码完美地适用于相关场景。

我想为另一个类似的场景调整相同的代码,我还没理解可能出错的地方。这一次,我有一个coursemodule集合,很多:课程和模块之间的许多关系集合,只存储coursesIdmoduleId。由于代码运行完美,我只是复制,做了一点修改,并得到了下面的代码:

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未定义:

L ... n.Cursor {collection:LocalCollection,sorter:null,matcher:M ... o.Matcher,_selectorId:undefined,skip:undefined ...} _projectionFn:(obj)_selectorId:undefined_transform:nullcollection:LocalCollectionfields:undefinedlimit:undefinedmatcher:Minimongo。 Matcherreactive:trueskip:undefinedsorter:null__proto__:Object_depend :( changers,_allow_unordered)_getCollectionName :()_ getRawObjects :( options)_publishCursor:(sub)constructor :( collection,selector,options)count :()fetch :()forEach :( callback ,thisArg)getTransform :()map :( callback,thisArg)observe :( options)observeChanges:(options)rewind :()proto:Object view.js:30 L ... n.Cursor {collection:LocalCollection,sorter:null, matcher:M ... o.Matcher,_selectorId:undefined,skip:undefined ...}

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

arrays mongodb meteor find meteor-blaze
2个回答
1
投票

您正在以错误的方式使用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}});

顺便说一下,如果你想直接查看findconsole.log函数返回的文档,请使用.fetch()

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

MongoDB查找功能文档:https://docs.mongodb.com/manual/reference/method/db.collection.find/


1
投票

@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();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.