One of the biggest issues I found with this IDs approach is the MongoDB pipeline, as long as you don't have to do complex pipeline lookup on that many IDs.
There is a memory threshold on aggregates I guess that's why mongoose uses plain queries on the populate function.
Hi,
"We can also store list of student IDs in course document. Then the time complexity should be N or o(1) as mongo creates indexes on id."
=> This makes the insert function more complicated ? If you want to add 1 students with 10 courses, you will must insert user_id to courses.student_list (apply with all of 10 courses) and insert 10 course_id to students.course.
Everything has its price, sometimes you have to accept the complexity?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Let's take students and course example..
You can create
Student{
_id:ID
name: String
courses: Array[ID]
}
Course{
_id:ID
name: String
}
Would it be slow to look up which students were in a particular course then, if there are, say, 100k students and 100k courses?
One of the biggest issues I found with this IDs approach is the MongoDB pipeline, as long as you don't have to do complex pipeline lookup on that many IDs.
There is a memory threshold on aggregates I guess that's why mongoose uses plain queries on the populate function.
Let say if you have N students and M courses and if we store course Id in sorted order then the time complexity should be N*log(M).
We can also store list of student IDs in course document. Then the time complexity should be N or o(1) as mongo creates indexes on id.
Hi,
"We can also store list of student IDs in course document. Then the time complexity should be N or o(1) as mongo creates indexes on id."
=> This makes the insert function more complicated ? If you want to add 1 students with 10 courses, you will must insert user_id to courses.student_list (apply with all of 10 courses) and insert 10 course_id to students.course.
Everything has its price, sometimes you have to accept the complexity?