我刚刚发现了 Mongoose 的 Populate Virtuals 方法,它将为我当前的项目节省大量时间。不过,我希望进一步扩展它。有没有一种基于多个本地/外键对进行填充的简单方法?这是代码可能是什么样子的示例(注意:这可能不是一个很好的示例,但希望它传达了我的问题的基础)。
var workerSchema = new Schema({
name: String,
locationCode: String,
departmentCode: String
});
var deparmentSchema = new Schema({
locationCode: String, //Note: neither location nor code
code: String, //are unique, but the combination of them are
manager: String,
otherInfoAboutDepartment: String
});
workerSchema.virtual('department', {
ref: "Department",
localField: ["locationCode", "departmentCode"],
foreignField: ["locationCode", "code"]
});
Run Code Online (Sandbox Code Playgroud)