I have a collection of the following sort of documents (taken from Mongo DB Docs: Schema Design for familiarity):
db.students
{ name: 'Jane',
scores: [
{ course: 'math', grade: 'A'},
{ course: 'biology', grade: 'B'},
{ course: 'english', grade: 'C'}
]
}
If I already have the document for Jane, what is the best way to retrieve her grade for e.g. math?
The only guidance I have been able to find in the docs concerns finding, for example, all the students with grade A for math, but in this case I already have the student document I want to query.
The only way I have seen to access arrays is through array index, e.g. scores[0].grade, but I do not necessarily know the array index.
Thanks
edit: I am aware that I could simply loop through the array to find the correct entry, but I was wondering if there was a better way.