一个模型的两个版本

时间:2018-03-19 16:27:23

标签: javascript node.js mongodb express

这有点奇怪,但我需要在node.js中有两个版本的相同模型。我会尝试尽力解释。

如果他们是求职者或雇主,我必须根据这个事实创建两种不同的acc类型。这些领域完全不同,我无法真正改变它们。

var mongoose = require('mongoose');
var plm = require('passport-local-mongoose');
var accountSchema = new mongoose.Schema({
  username: String,
  accType: String,
  companyName: String,
  contactPersonFullName: String,
  email: String,
  companyWebsite: String,
  city: String,
  province: String,
  postalCode: String,
  phoneNumber: String,
  hiringRegion: String[], // TODO
  description: String,
  logo: String, //TODO
  workingWithEOESC: Boolean,
  industry: String,
});
accountSchema.plugin(plm);
module.exports = mongoose.model('account-employer', accountSchema);

这是我的雇主帐户模型。这是我的求职者模型:

var mongoose = require('mongoose');
var plm = require('passport-local-mongoose');
var accountSchema = new mongoose.Schema({
  username: String,
  accType: String,
  city: String,
  province: String,
  postalCode: String,
  phone: String,
  ageGroup: String,
  education: String,
  lookingForWork: Boolean,
  employmentStatus: String,
  workingWithEOESC: Boolean,
  resume: String, //TODO
  mainWorkExp: String,
});
accountSchema.plugin(plm);
module.exports = mongoose.model('account-seeker', accountSchema);

我认为这是一个好主意,但之后我意识到我需要one帐户模型,因为passport在Express中的工作原理。如何确保不同的用户类型在NoSQL数据库中具有不同的字段和信息,同时保持一个帐户登录并注册?我现在可能没有任何意义,但我无法找到摆脱这种情况的方法。

1 个答案:

答案 0 :(得分:1)

var mongoose = require('mongoose');
var plm = require('passport-local-mongoose');
var userSchema = new mongoose.Schema({
  accType: String,
  companyName: String,
  contactPersonFullName: String,
  email: String,
  companyWebsite: String,
  city: String,
  province: String,
  postalCode: String,
  hiringRegion: String[], // TODO
  description: String,
  logo: String, //TODO
  workingWithEOESC: Boolean,
  industry: String,
  phone: String,
  ageGroup: String,
  education: String,
  lookingForWork: Boolean,
  employmentStatus: String,
  resume: String, //TODO
  mainWorkExp: String,
  isEmployer: Boolean // added this one to distinguish between employer and employee
});
userSchema.plugin(plm);
module.exports = mongoose.model('User', userSchema);

为什么不将它们组合成一个模式,再添加一个字段来检查这是否是雇员和雇主。