如何在meteor + mongoDB中找到使用id并返回该数组中的元素

时间:2016-03-17 15:42:58

标签: mongodb meteor

如何找到使用"_id": "WTyrTJqvf63JHQbcB""courseId": "5dWwpBp5ye492Bt4F"并仅返回状态?

这不起作用

        let clas = Col_AllClasses.findOne({
            _id: classData.classId,
            "courseList.courseId": classData.courseIdList[j]
        }, {
            courseList: {
                "$elemMatch": {
                    courseId: classData.courseIdList[j]
                }
            }
        })

MongoDB的

    {
      "_id": "WTyrTJqvf63JHQbcB",
      "title": "haha2",
      "school": "innervative",
      "password": "123",
      "courseList": [
        {
          "courseId": "5dWwpBp5ye492Bt4F",
          "status": 0
        },
        {
          "courseId": "sHhABCzPfkzGjdcK8",
          "status": 1
        }
      ],
      "studentIdList": []
    },
    {
      "_id": "9Pyu3QGwF6KmWAjjb",
      "title": "haha1",
      "school": "innervative",
      "password": "123",
      "courseList": [
        {
          "courseId": "sHhABCzPfkzGjdcK8",
          "status": 0
        },
        {
          "courseId": "AiEdf4FWyQkwJAnv3",
          "status": 1
        },
        {
          "courseId": "5dWwpBp5ye492Bt4F",
          "status": 0
        }
      ],
      "studentIdList": []
    }

1 个答案:

答案 0 :(得分:0)

我建议只按ID获取文档,然后浏览课程列表。这是一个例子:

let classId = 'WTyrTJqvf63JHQbcB';
let courseId = '5dWwpBp5ye492Bt4F';
let clas = Col_AllClasses.findOne(classId);
let { status } = _.findWhere(clas.courseList, { courseId });
console.log(status);