因此,基本上,在我的应用程序中,我有一个员工和一个公司模型。这只是有关这些模型的基本信息,实际上还有更多信息,因此使用嵌套对象而不是2模式似乎不是一个好选择(我认为)
var EmployeeSchema = new Schema(
{
name: { type: String, required: true, max: 100 },
company: { type: Schema.Types.ObjectId, ref: 'Company', required: true },
}
);
var CompanySchema = new Schema(
{
name: { type: String, required: true },
},
{
toJSON: { virtuals: true },
},
);
CompanySchema.virtual('employees', {
ref: 'Employee',
localField: '_id',
foreignField: 'company',
justOne: false,
});
在创建新员工的表单上,我希望选择一个公司,或者创建一个新公司。
所以我的API将发送如下信息:
employee: {
name: 'John Bastien',
company: 5d44635fa5993c0424da8e07
}
或:
employee: {
name: 'Dan Smith',
company: {
name: 'ACME'
}
}
这当然可以更改,这正是我的初衷。
因此,在我的快速应用中,当我做var employee = await new Employee(req.body.employee).save();
时,该如何做才能与员工一起创建公司。发送对象ID时,它工作正常,但是如何仅使用关联文档的JSON对象来实现它?
答案 0 :(得分:0)
我最终在模型上写了一些中间件来解决这个问题。可以提取此逻辑以使其更通用,但是对于我的用例而言,还不需要。
EmployeeSchema.virtual('company', {
ref: 'Company',
localField: 'companyId',
foreignField: '_id',
justOne: true,
}).set(function(company) {
this.companyId= company._id;
this.$company = company;
return this.$company;
});
EmployeeSchema.pre('validate', function(next) {
if (this.company && this.company instanceof Company) {
var err = this.company.validateSync();
if (err) {
// mergeErrors is a helper function that will merge the two exceptions into a nice format
err = mergeErrors(this.validateSync(), { company: err });
}
next(err);
}
next();
});
EmployeeSchema.pre('save', async function(next, saveOpts) {
if (this.company && this.company instanceof Company && this.company.isModified()) {
await this.company.save(saveOpts);
}
next();
});