Hi there,
I’m new to NoSQL databases and MongoDB and have some troubles understanding some core schema-design principles, despite the very well structured MongoDB’s docs and similar answers all around the i-net.
In SQL world, there are Linked tables
, mostly used as many-to-many
references between other tables, in MongoDB, I didn’t find any doc entry or tutorial equivalent to them. The only many-to-many
solution I found is to link each connected tables to each other via DBRefs
:
So, for example if I have Students
and Courses
, I will do(using mongoose):
const StudentSchema = new Schema({
name: String,
courses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }]
});
const CoursesSchema = new Schema({
name: String,
studends: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Student' }]
});
And for me it’s a very bad schema-design, because it’s not scalable for the reason the array of students
and courses
can grow very large(student-courses was just an example). So from scalability perspective, it’s bad.
Are there any alternatives to that?
What if I’ll use a linked tables
(collection
) to connect between the two, how should I implement it on the application level: should I first make a call to the linked collection
, await it and only then perform another query? Can I do it in one go(make one request to the DB server), because awaiting for the first response on the application level can take time?
Hopefully, you could assist with the above concerns of mine.
Thanks