我有这样的架构:
class Schemas
constructor: ->
@mongoose = require 'mongoose'
@schema = @mongoose.Schema
@EmployeeSchema = new @schema
'firstname': { type: String, required: true },
'lastname': { type: String, required: true },
'email': { type: String, required: true, index: { unique: true }, validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/ },
'departmentId': { type: @schema.ObjectId, required: true }
'enddate': String,
'active': { type: Boolean, default: true }
@EmployeeSchemaModel = @mongoose.model 'employees', @EmployeeSchema
@DepartmentSchema = new @schema
'name': { type: String, required: true, index: { unique: true } }
'employees' : [ @EmployeeSchema ]
@DepartmentSchemaModel = @mongoose.model 'departments', @DepartmentSchema
这样我就employees
活在一个文件数组employee
里面了department
我有几个department
文档,其中有许多employee
文档存储在employees
数组中。
然后我添加了一个新的department
,但它不包含employees
. 如果我然后尝试添加另一个department
没有employees
,Mongoose 会Duplicate key error
为该employee.email
字段生成一个必填字段。该employee.email
字段是必需的且唯一的,并且必须如此。
反正有这个吗?