How to make 2 consecutives $ group

hello,I don’t understand why it doesn’t work??:

Moddulle.aggregate([
    {
      $unwind: "$list_epreuves"
    },
    {
      $unwind: "$list_epreuves.resultat"
    },
    {
      $group: {
        _id: {
          nom: "$list_epreuves.resultat.nom",
          prenom: "$list_epreuves.resultat.prenom"
        },
        cursus: {
          $push: {
            designation_moddulle: "$designation_moddulle",
            pv_modulaire: {
              $push:{
                Code_epreuve: "$list_epreuves.Code_epreuve",
                valeur_note: "$list_epreuves.resultat.valeur_note"
              
            }}
          }
        }
      }
    },
    {
      $project: {
        _id: 0,
        nom: "$_id.nom",
        prenom: "$_id.prenom",
        cursus: 1
      }
    },
   
  ])

it send me : MongoServerError: Unrecognized expression ‘$push’

Hello @Amina_Mesbah,

The $group won’t allow a nested $push operator, you need two groups, the first group by your designation_moddulle and get an array of pv_modulaire and the second group is to prepare cursus array,

    {
        $group: {
            _id: {
                nom: "$list_epreuves.resultat.nom",
                prenom: "$list_epreuves.resultat.prenom",
                designation_moddulle: "$designation_moddulle"
            },
            pv_modulaire: {
                $push:{
                    Code_epreuve: "$list_epreuves.Code_epreuve",
                    valeur_note: "$list_epreuves.resultat.valeur_note"
                }
            }
        }
    },
    {
        $group: {
            _id: {
                nom: "$_id.nom",
                prenom: "$_id.prenom"
            },
            cursus: {
                $push: {
                    designation_moddulle: "$_id.designation_moddulle",
                    pv_modulaire: "$pv_modulaire"
                }
            }
        }
    },

Haven’t tested the query but you can do something like this.

2 Likes

thank you,here is the result of the query:

[
 {
   cursus: [ [Object], [Object], [Object], [Object] ],
   nom: 'smith',
   prenom: 'jack'
 },
 {
   cursus: [ [Object], [Object], [Object], [Object] ],
   nom: 'doe',
   prenom: 'john'
 }
]

in the cursus field I would like to have the epreuve’s result of each module

                                                            [{designation_moddulle:physique,
                                                             pv_modulaire:
                                                                 {
                                                                   code_epreuve: physique_emd1,
                                                                   valeur_note: 10},
                                                                {code_epreuve: physics_emd2,
                                                                 valuer_note: 14}
                                                          }]

Hi @Amina_Mesbah,

Can you please provide the example existing documents and the expected result from that documents?

Hi @turivishal ,i wanna have for each student and for each module all the marks he had at each epreuve of the module to finally be able to calculate the average of each module then the overall average

[
  {nom: 'smith',
    prenom: 'jack',
    cursus: [ {designation_moddulle:physique
               pv_modulaire:[{code_epreuve:physique_emd1,
                             valeur_note:15,50},
                             {code_epreuve:physique_emd2,
                             valeur_note:11,00}]},
              {designation_moddulle:biologie
               pv_modulaire:[{code_epreuve:biologie_emd1,
                             valeur_note:17,50},
                             {code_epreuve:biologie_emd2,
                             valeur_note:10,50}]} ]
    
  },
  {nom: 'doe',
    prenom: 'john',
    cursus: [ {designation_moddulle:physique
               pv_modulaire:[{code_epreuve:physique_emd1,
                             valeur_note:08,50},
                             {code_epreuve:physique_emd2,
                             valeur_note:18,00}]},
              {designation_moddulle:biologie
               pv_modulaire:[{code_epreuve:biologie_emd1,
                             valeur_note:13,50},
                             {code_epreuve:biologie_emd2,
                             valeur_note:19,00}]} ],
    
  }
]



with your solution I have this result;

[
  {
    cursus: [ [Object], [Object], [Object], [Object] ],
    nom: 'smith',
    prenom: 'jack'
  },
  {
    cursus: [ [Object], [Object], [Object], [Object] ],
    nom: 'doe',
    prenom: 'john'
  }
]

thank’s

Hi @Amina_Mesbah,

Where is the actual document that exists in your collection? i can’t predict on the base of the expected result.

hi @turivishal ,here is the document:

{"_id":{"$oid":"647a5f3d5f4e7d2f9b781c62"},
"code_moddulle":"97",
"designation_moddulle":"physique",
"coefficient":{"$numberInt":"2"},
"nombre_epreuves":{"$numberInt":"2"},
"année":{"$numberInt":"1"},
"listEpreuves":
	[{"code_epreuve":"physique_emd1",
	"date_epreuve":{"$date":{"$numberLong":"1686441600000"}},
	"année_epreuve":{"$numberInt":"1"},
	"nature_epreuve":"EMD",
	"resultat":[{
		"nom_etudiant":"laouar",
		"prenom_etudiant":"hocine",
		"valeur_note":{"$numberDouble":"13.283"},
		"_id":{"$oid":"647b21c9c868ea40c8942eec"}},
		{"nom_etudiant":"bouzenda",
		"prenom_etudiant":"khaled",
		"valeur_note":{"$numberDouble":"12.783"},
		"_id":{"$oid":"647b21c9c868ea40c8942eed"}}],
	"_id":{"$oid":"647abd98789a4a1e3ae735ba"}}],
	"__v":{"$numberInt":"0"}}

Hi @Amina_Mesbah ,

The properties are totally different than you used in your query,

The nom and prenom are not exists in your document.

I can’t help you with this incomplete information.

1 Like

no it is not the problem because i arranged them in my code. In any case, i have the values they just aren’t being printed .
thank you

that’s what i had to do:
console.log(JSON.stringify(result))
thank’s @turivishal

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.