I have read many articles, posts, and stack overflow solutions for understanding how to reference one collection from another collection using JSON schema validation in MongoDB. I would appreciate it if you can give the solution to the problem with an example of how referencing works in JSON schema validation for MongoDB
Note: This is not an actual data model but something similar to the problem I am trying to solve
Q1) I have students' data and each student can have many courses.
The student collection JSON schema is:
db.createCollection("student",{
validator: { $jsonSchema: {
bsonType: "object",
required:["name", "dob", "course"],
properties: {
name :{
bsonType: "string",
maxLength:40,
},
age :{
bsonType: "int",
maxLength:3,
},
dob:{
bsonType: "string",
},
course:
{
### reference the courses collection to store data
},
}
}
}
)
and the courses collection JSON schema is this:
db.createCollection("course",{
validator: { $jsonSchema: {
bsonType: "object",
properties: {
name :{
bsonType: "string",
maxLength:40,
},
instructor:{
bsonType: "string",
maxLength:40,
},
credits{
bsonType : "int",
maxLength: 2
}
}
}
}
)
I do not want to embed the course collection in the student collection but reference the course collection from the student collection. How can I do this in JSON schema for Mongo DB since $ref cannot be used to link two collections in JSON schema in MongoDB?
I would also appreciate links explaining the solution to this problem with a SCHEMA DESIGN example.
Note: I have gone through the whole MongoDB documentation, and JSON schema documentation to find a solution to this problem.