I'm learning mongodb with mongoose (normalized documents) and trying to understand how to fetch data accross referenced document
const employeeSchema = new Schema({
name: String,
email: String,
});
const ContactSchema = new Schema({
email: String,
mobile: String,
fax: String,
});
const DepartmentSchema = new Schema({
contact: ContactSchema,
title: String,
director: {
type: Schema.Types.ObjectId,
ref: 'Employees',
},
});
const ChannelSchema = new Schema({
title: String,
});
const StudentSchema = new Schema({
channel: {
type: Schema.Types.ObjectId,
ref: 'Channels',
},
department: {
type: Schema.Types.ObjectId,
ref: 'Departments',
},
name: {
type: String,
},
});
const Students = model('Students', StudentSchema);
const Departments = model('Departments', DepartmentSchema);
const Channels = model('Channels', ChannelSchema);
const Employees = model('Employees', employeeSchema);
How to achieve a query like below in mongoose
Students.find({ name: 'studentName' }); //Ok
Students.find({ channel: { name: 'channelName' } });
Students.find({ department: { director: { name: 'directorName' } } });
Students.find({ department: { contact: { email: 'emailDirector' } } });
The first line work as expected