mongoose查询子集的子集

jsliang
  • 293

首先,我的mongoose的models如下:

var clubsSchema = new mongoose.Schema({
    "clubCreatetime": String,
    "clubCreater": String,
    "clubName": String,
    "clubDescription": String,
    "clubImage": String,
    "clubMenbers": [String],
    "clubTopics": [{
        "topicCreatetime": String,
        "topicCreater": String,
        "topicTitle": String,
        "topicContent": String,
        "topicComment": [{
            "userId": String,
            "userName": String,
            "commentCreatetime": String,
            "commentContent": String
        }],
        "topicLike": [String],
        "topicState": String
    }],
    "clubState": String
})

然后,我的代码如下:

let orgId = req.param("orgId");
  let topicId = req.param("topicId");
  Club.findOne({
    _id: orgId
  }, function(clubErr, clubDoc){
    //console.log(clubDoc);
    if(clubErr){
      res.json({
        status: '1',
        msg: clubErr.message,
        result: '查找该社团失败!'
      })
    } else {
      clubDoc.clubTopics.findOne({
        _id: topicId
      }, function(topicErr, topicDoc){
        console.log(topicDoc);
        if(topicErr){
          res.json({
            status: '1',
            msg: topicErr.message,
            result: '查找该话题失败!'
          })
        } else {
          res.json({
            status:'0',
            msg: '查找该话题成功!',
            result: topicDoc
          })
        }
      })
    }
  })

在这里,我是想通过路由中的社团Id,先找到该社团;然后在根据路由中的话题Id,去查询该话题全部内容。但是,它提示我.findOne()这方法不存在,所以想求教下,我要根据id查询社团下的该话题,要怎么写呢?谢谢!

目前想到的返回话题信息的方法是:

for(let i=0; i<clubDoc.clubTopics.length; i++){
    if(clubDoc.clubTopics[i]._id==topicId){
        console.log(clubDoc.clubTopics[i]);
    }
}

想问下mongoose有没有更直接的方法???

回复
阅读 2.2k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏