Найти количество с помощью методов arr - PullRequest
0 голосов
/ 04 октября 2019

Мне нужно найти решение на основе агрегации MongoDB.

Я новичок в MongoDB. Я попробовал следующие этапы:

[
  {
    '$lookup': {
      'from': 'exams', 
      'localField': '_id', 
      'foreignField': 'quizRef', 
      'as': 'exams'
    }
  }, {
    '$addFields': {
      'examCount': '$exams.status'
    }
  }
]

Здесь мой объект MongoDB выглядит так, что это только один объект, который я хочу применить к нескольким объектам, т.е. массиву объектов

{
    _id: ObjectId("5d832ea380ffae26cf813bb3"),
    skillsRef: Array,
    questionsRef: Array,
    name: "MCQ+PROGRAMMING QUIZ!",
    summary: "This is the quiz!!!!!",
    difficultyLevel: "EASY",
    mcqPassingCriteria: 50,
    programmingPassingCriteria: 50,
    mcqPoints: 35,
    programmingPoints: 25,
    mcqTime: Object,
    programmingTime: Object,
    difficultyLevelMatrix: Array,
    skillsMatrix: Array,
    userRef: ObjectId("5d2d644bbba7db1252ef85e0"),
    createdAt: 2019 - 09 - 19 T07: 30: 43.840 + 00: 00,
    updatedAt: 2019 - 09 - 19 T10: 58: 41.038 + 00: 00,
 }

После применения вышефактический результат запроса агрегации:

{
    _id: ObjectId("5d832ea380ffae26cf813bb3"),
    skillsRef: Array,
    questionsRef: Array,
    name: "MCQ+PROGRAMMING QUIZ!",
    summary: "This is the quiz!!!!!",
    difficultyLevel: "EASY",
    mcqPassingCriteria: 50,
    programmingPassingCriteria: 50,
    mcqPoints: 35,
    programmingPoints: 25,
    mcqTime: Object,
    programmingTime: Object,
    difficultyLevelMatrix: Array,
    skillsMatrix: Array,
    userRef: ObjectId("5d2d644bbba7db1252ef85e0"),
    createdAt: 2019 - 09 - 19 T07: 30: 43.840 + 00: 00,
    updatedAt: 2019 - 09 - 19 T10: 58: 41.038 + 00: 00,
    exams: [{
        _id: ObjectId('5d84666f58e4db4a5f4ce7b0'),
        status: "RESULT",
        candidateDetail: Object,
        campaignId: "GA0004905",
        quizRef: ObjectId('5d832ea380ffae26cf813bb3'),
        gitHubLink: Array,
        selectedAnswers: Array,
        createdAt: 2019 - 09 - 20 T05: 41: 03.710 + 00: 00,
        updatedAt: 2019 - 09 - 20 T06: 00: 27.322 + 00: 00,
        mcqPassingCriteriaStatus: "FAIL",
        mcqPoints: 0,
        programmingPassingCriteriaStatus: "FAIL",
    }, {
        _id: ObjectId('5d846c8eb515022b3836da4b'),
        status: "UNDER_EVALUATION",
        candidateDetail: Object,
        campaignId: "GA0004905",
        quizRef: ObjectId('5d832ea380ffae26cf813bb3'),
        token: "799d86d3-c901-47b3-afe9-becc34e171d8",
        gitHubLink: Array,
        selectedAnswers: Array,
        createdAt: 2019 - 09 - 20 T06: 07: 10.959 + 00: 00,
        updatedAt: 2019 - 09 - 20 T11: 01: 08.282 + 00: 00,
        mcqPoints: 35,
        programmingPoints: 53,
        programmingPassingCriteriaStatus: "PASS",
    },{
        _id: ObjectId('5d846c8eb515022b3836da4c'),
        status: "UNDER_EVALUATION",
        candidateDetail: Object,
        campaignId: "GA0004905",
        quizRef: ObjectId('5d832ea380ffae26cf813bb3'),
        token: "799d86d3-c901-47b3-afe9-becc34e171f8",
        gitHubLink: Array,
        selectedAnswers: Array,
        createdAt: 2019 - 09 - 20 T06: 07: 10.959 + 00: 00,
        updatedAt: 2019 - 09 - 20 T11: 01: 08.282 + 00: 00,
        mcqPoints: 35,
        programmingPoints: 53,
        programmingPassingCriteriaStatus: "PASS",
    }],
    examCount: ["RESULT", "UNDER_EVALUATION","UNDER_EVALUATION"]
}

Ожидаемый результат:

{
    _id: ObjectId("5d832ea380ffae26cf813bb3"),
    skillsRef: Array,
    questionsRef: Array,
    name: "MCQ+PROGRAMMING QUIZ!",
    summary: "This is the quiz!!!!!",
    difficultyLevel: "EASY",
    mcqPassingCriteria: 50,
    programmingPassingCriteria: 50,
    mcqPoints: 35,
    programmingPoints: 25,
    mcqTime: Object,
    programmingTime: Object,
    difficultyLevelMatrix: Array,
    skillsMatrix: Array,
    userRef: ObjectId("5d2d644bbba7db1252ef85e0"),
    createdAt: 2019 - 09 - 19 T07: 30: 43.840 + 00: 00,
    updatedAt: 2019 - 09 - 19 T10: 58: 41.038 + 00: 00,
    exams: [{
        _id: ObjectId('5d84666f58e4db4a5f4ce7b0'),
        status: "RESULT",
        candidateDetail: Object,
        campaignId: "GA0004905",
        quizRef: ObjectId('5d832ea380ffae26cf813bb3'),
        gitHubLink: Array,
        selectedAnswers: Array,
        createdAt: 2019 - 09 - 20 T05: 41: 03.710 + 00: 00,
        updatedAt: 2019 - 09 - 20 T06: 00: 27.322 + 00: 00,
        mcqPassingCriteriaStatus: "FAIL",
        mcqPoints: 0,
        programmingPassingCriteriaStatus: "FAIL",
    }, {
        _id: ObjectId('5d846c8eb515022b3836da4b'),
        status: "UNDER_EVALUATION",
        candidateDetail: Object,
        campaignId: "GA0004905",
        quizRef: ObjectId('5d832ea380ffae26cf813bb3'),
        token: "799d86d3-c901-47b3-afe9-becc34e171d8",
        gitHubLink: Array,
        selectedAnswers: Array,
        createdAt: 2019 - 09 - 20 T06: 07: 10.959 + 00: 00,
        updatedAt: 2019 - 09 - 20 T11: 01: 08.282 + 00: 00,
        mcqPoints: 35,
        programmingPoints: 53,
        programmingPassingCriteriaStatus: "PASS",
    },{
        _id: ObjectId('5d846c8eb515022b3836da4c'),
        status: "UNDER_EVALUATION",
        candidateDetail: Object,
        campaignId: "GA0004905",
        quizRef: ObjectId('5d832ea380ffae26cf813bb3'),
        token: "799d86d3-c901-47b3-afe9-becc34e171f8",
        gitHubLink: Array,
        selectedAnswers: Array,
        createdAt: 2019 - 09 - 20 T06: 07: 10.959 + 00: 00,
        updatedAt: 2019 - 09 - 20 T11: 01: 08.282 + 00: 00,
        mcqPoints: 35,
        programmingPoints: 53,
        programmingPassingCriteriaStatus: "PASS",
    }],
    examCount: [{status:"RESULT",count:1},{status:"UNDER_EVALUATION",count:2}]
}

Мне нужно подсчет статуса и согласно массиву экзаменов. Я не знаю, как применить запрос к полю экзаменов и найти желаемый результат. Мне нужно найти результат в массиве экзаменов и добавить счетчик в поле count.

...