0

I would like to display the list of students who have passed at least 3 courses at ENSA with more than 60.

db.etudiants.insert({
    nom: 'emile',
    prenom: 'josé',
    cours_reussis: [{
        nom_cours: 'SO2',
        periodes: NumberInt(60),
        note: 70,
        date_reussite: NumberInt(2015),
        dispense: true,
        nom_ecole: 'ENSA'
    }, {
        nom_cours: 'IBD',
        periodes: NumberInt(60),
        note: 80,
        date_reussite: NumberInt(2015),
        dispense: true,
        nom_ecole: 'ENSA'
    }, {
        nom_cours: 'GEB',
        periodes: NumberInt(60),
        note: 65,
        date_reussite: NumberInt(2016),
        dispense: true,
        nom_ecole: 'ENSA'
    }]
})

db.etudiants.insert({
    nom: "larhemouchi",
    prenom: "zouhair",
    cours_reussis: [
      {
        nom_cours: "PAI",
        periodes: NumberInt(60),
        note: 68,
        date_reussite: NumberInt(2015),
        dispense: true,
        nom_ecole: "ENSA"
      }, 
      {
        nom_cours: "GEB",
        periodes: NumberInt(60),
        note: 78,
        date_reussite: NumberInt(2014),
        dispense: true,
        nom_ecole: "ENSA"
      }
    ]
})

In truth I tried and I am a bit ready to answer but I do not know how to have the students who have successfully 3 subjects.

here is my answer

  db.etudiants.aggregate([
  {
    $project: {
        _id: 1,
        'nom': 1,
        'prenom': 1,
        'cours_reussis': 1
    }
  }, 
  {
    $unwind: '$cours_reussis'
  }, 
  {
    $match: {
        'cours_reussis.note': {
            $gt: 60
        },
        'cours_reussis.nom_ecole': "ENSA"
    }
  }, 
  {
    $group: {
        '_id': {
            'nom': '$nom',
            'prenom': '$prenom',
            'cours_reussis.note': '$cours_reussis.note',
            'cours_reussis.nom_ecole': '$cours_reussis.nom_ecole'
        },
    }
  }
])

I would just like the students who have successfully taken 3 courses. In our database, the student who has successfully completed 3 courses is "emile josé".

0

1 Answer 1

1

Try the following:

db.etudiants.aggregate([
 {
   $unwind: '$cours_reussis'
 }, 
 {
   $match: {
       'cours_reussis.note': {
           $gt: 60
       },
       'cours_reussis.nom_ecole': "ENSA"
   }
 }, 
 {
   $group: {
       '_id': {
           'nom': '$nom',
           'prenom': '$prenom',
       }, 
       'num_courses' : { "$sum" : 1 }
   }
 }, 
 { $match : { 
      'num_courses' : { '$gte' : 3 }
  }
 }
])

Does this meet your criteria?

Note that the only changes I made to your aggregation were the following:

  • I limited the '_id' to just the 'nom' and 'prenom' fields
  • I used $sum to count the number of completed courses
  • The initial $projection stage was removed, as the aggregation can perform this optimization automatically.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.