I doing migration from mysql to mongodb. Now I get confused how to transform below schema to MongoDB or How to query courses in a college. mysql:
college(college_id,name)
course_college(college_id,course_id)
course(course_id,name)
mongo:
college collection
{
"college_id" : "1",
"name" : "Oxford ",
}
relation:
{
"college_id" : "1",
"course_id" : "1",
}, {
"college_id" : "9",
"course_id" : "67",
},
course collection
{
"course_id" : "1",
"name" : "Master of Computer Applications ",
}
{
"course_id" : "2,
"name" : "Bachelor of Computer Applications ",
}
I need to mongoose query get course in a college for this relationship. then i convert into non relational schema.
MongoDB is not a relational database, so it is not a suitable technology when you have relational data. When you decide to migrate from a relational database to MongoDB (usually not a good idea), you need to change the way you structure your data.
A relations-collection in a document database is usually not a good idea because getting data from more than one collection can not be done in a single query like it can be done with a JOIN in a relational database.
A better solution would be to either create an array-field in your college-document which has all the courses that college offers or create an array-field in your course-document which embeds all the colleges. Which option you choose depends on which use-case is more important in your application: listing one college with all of its courses or listing one course with all colleges which offer it. When both use-cases are important, you might consider to do both.
Note that you don't necessarily need to embed the whole documents. Your option range from embedding the whole document, embedding just the most important fields of embedding just the _id so that all further information needs to be obtained with an additional query. Which choice you make in this regard depends on which fields of the sub-documents are required to fulfill your most common use-cases.
Thank you @Philipp
Here MongoDB works like MySQL Relation.
I tried a solution for my emergency
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CollegeSchema = mongoose.Schema({
college_name : String
});
var College = mongoose.model('College', CollegeSchema);
var CourseCollegeSchema = mongoose.Schema({
college_id : {type: Schema.ObjectId, ref: 'College' }
, course_id : {type: Schema.ObjectId, ref: 'Course' }
});
var CourseCollege = mongoose.model('CourseCollege', CourseCollegeSchema);
var CourseSchema = mongoose.Schema({
course_name : String
});
var Course = mongoose.model('Course', CourseSchema);
Queries
// For searching colleges related to a course
CourseCollegeSchema.find({college_id:"53f9ee7ac3130d4816b858ef"}).populate('course_id').exec(function(err, courses){console.log(courses);})
// For searching colleges related to a course
CourseCollegeSchema.find({course_id:"53f9efe4b6ba1a8022f6acdd"}).populate('college_id').exec(function(err, colleges){console.log(colleges);})