Does it make sense to index the parameter with an array value?

I have a parameter in my Lesson Schema, “students”. The “students” parameter is an array of studentSchema containing, enrolledDate, student_id (referencing the User Collection) and an attendance parameter.

i am thinking of indexing the student_id parameter in the students array, in the Lesson Schema.

Reason being when a student logs in he/she should be able to see all the lessons he/she needs to attend and it makes sense to me that i should index this parameter so that when i query with $in the dbms would retrieve the lessons much quicker.

Not so confident in my understanding of how mongodb dbms works. Please comment. Thank you.

Hi @Kenneth_Goh ,

Yes, if you intend to query on a subdocument field (in an array or subdocument) you should index it.

So for queries like :

db.lessons.find({"students.student_id" : "xxx"}) 

Index "students.student_id" : 1 should do the job

Recommend reading

Thanks
Pavel

2 Likes