javascript - 猫鼬过滤文档
问题描述
我正在使用 mongoose(规范化文档)学习 mongodb,并试图了解如何跨引用文档获取数据
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);
如何在猫鼬中实现如下查询
Students.find({ name: 'studentName' }); //Ok
Students.find({ channel: { name: 'channelName' } });
Students.find({ department: { director: { name: 'directorName' } } });
Students.find({ department: { contact: { email: 'emailDirector' } } });
第一行按预期工作
解决方案
推荐阅读
- ruby-on-rails - 为什么我的记录保存后全是零字段?
- python - Plotting a curve from dots of a scatter plot
- r - separte columns from a data frame
- javascript - Print value in more than one const statement
- firebase - Firestore: Query a collection for all documents user has access to
- kubernetes - unable to get the system service memory and cpu metrics of kubernetes cluster in grafana dashboard using prometheus
- typescript - 如何在 typescript 模块中扩展构建类,如 HTMLElement 或 Node?
- vert.x - Creating multiple HTTP servers on the same host/port
- oracle - 定义一个具有多个值的变量
- google-sheets - 根据另一个单元格的值选择单元格