当引用另一个猫鼬模式时,我必须使UserType唯一。

时间:2019-11-14 06:27:00

标签: mongodb mongoose mongoose-schema mongoose-populate

我有以下两个模式IndustryModel.js和userModel.js

industryModel.js

const mongoose = require("mongoose");

const industryTypeSchema = new mongoose.Schema({
  industryType: {
    type: String,    // example-- Supplier, Customer, Company
    required: [true, "Please provide a industry type"],
    unique: true
  },

module.exports = IndustryType = mongoose.model(
  "IndustryType",
  industryTypeSchema
);

userModel.js

const mongoose = require("mongoose");

const userTypeSchema = new mongoose.Schema({
  userType: {
    type: String, //  example--Manager, Engineer, Staff, Employee
    required: [true, "Please provide a user type"]
    unique: true  
  },

  industryType: {
    type: mongoose.Schema.ObjectId,
    ref: "IndustryType",
    required: [true, "Provide Indsutry Type"]
  }
});

userTypeSchema.pre(/^find/, function(next) {
  this.populate({
    path: "industryType",
    select: "industryType"
  });
  next();
});

module.exports = UserType = mongoose.model("UserType", userTypeSchema);

我的问题是我想创建的不是唯一的用户类型,而是与其行业类型相关的每个用户类型。 例如,有3种行业类型(供应商,客户,公司),我想为每三个行业(供应商,客户,公司)创建经理,但对于每个类似供应商的经理都有2个经理。

请帮助我如何实现此目标,或者我必须更改Schema。您的回应对我来说非常有价值,因为我的猫鼬并不新鲜

0 个答案:

没有答案
相关问题