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

我有以下两个模式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。您的回应对我来说非常有价值,因为我的猫鼬并不新鲜

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

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3104881.html

大家都在问