0

here is the model of my collection :

classes:[{
            class:String,
            info:{
                    subjects:[String],
                    students:[{
                        name:String,
                        surname:String,
                        matriculae:Number,
                        path_1:String,
                        path_2:String
                    }],
                    classTeacher:{
                        name:String,
                        surname:String
                    }                   
                }
            }],
    accademicYear:String}];  

I'd like to retrive value 'matriculae' given the name,surname and accademicYear of a student. I cant wrap my head 'round it tho! Thanks for help.

1 Answers1

0

If you mean you want the result flat format, try this:

 School.aggregate([
    {
        $unwind: '$classes'
    }, {
        $project: {
            accademicYear: 1,
            students: "$classes.info.students"
        }
    }, {
        $unwind: "$students"
    }, {
        $project: {
            accademicYear: 1,
            matriculae: "$students.matriculae",
            name: "$students.name",
            surname: "$students.surname",
        }
    }
    ])

In case of the classes is collection and accademicYear is inside of the classes collection.Plus added the match criteria.

db.getCollection('classes').aggregate([{
    $project: {
        accademicYear: 1,
        students: "$info.students"
    }
}, {
    $unwind: "$students"
}, {
    $project: {
        accademicYear: 1,
        matriculae: "$students.matriculae",
        name: "$students.name",
        surname: "$students.surname",
    }
}, {
    $match: {
        name: name,
        surname: surname,
        accademicYear: year

    }

}])
Semih Gokceoglu
  • 1,408
  • 1
  • 13
  • 21
  • school beeing the collections name? –  May 05 '17 at 16:04
  • Yeah. Classes is subarray of school collection. In your case classes is a collection? – Semih Gokceoglu May 05 '17 at 17:56
  • classes is a collection, tho with the code above all the students from that document are returned. Formatted according to the last projection. I'd like to be returned just the one students that matches the criteria (name:value, surname:value) inside the document classes that matches (accademicYear: value) of the whole collections –  May 06 '17 at 10:41
  • If so, your model is wrong. "academicYear" and "classes" are the same level of the model. If "classes" field is the collection, academicYear has to be inside of the model. Please fix your model first. The second question you can add $match field into aggregation method. I can add it after you fix the model. – Semih Gokceoglu May 08 '17 at 13:58
  • Sorry for ignoring your efforts, i was busy. I'll see if something's wrong with the model. But i've read on another question [link](http://stackoverflow.com/questions/10043965/how-to-get-a-specific-embedded-document-inside-a-mongodb-collection) that it is not possible to retrive a field from a specific document, the full document is returned. I've worked around it by simply iterating over the results on the server, looking for a match on the result and picking the field i wanted. –  May 08 '17 at 15:10
  • Yes, you can retrieve specific field using aggregation method, but yes it's not possible with the only find method. Try my last edited answer. You will be able to retrieve documents you want. – Semih Gokceoglu May 08 '17 at 15:45