首页 > 解决方案 > 猫鼬同时创建孩子和关联的父母

问题描述

所以基本上,在我的应用程序中,我有一个员工和一个公司模型。这只是关于这些模型的基本信息,实际上还有更多信息,所以使用嵌套对象而不是 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 对象呢?

标签: node.jsmongodbmongoose

解决方案


我最终在我的模型上编写了一些中间件来处理这个问题。可以提取此逻辑以使其更通用,但对于我的用例,它还不需要。

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();
});

推荐阅读