【问题标题】:Problem with “Employee with independent job income” schema design with mongoosemongoose 的“有独立工作收入的员工”模式设计的问题
【发布时间】:2020-08-15 14:59:17
【问题描述】:

学习 mongoDb。寻找正确的架构解决方案: 有员工,每个员工都有自己的工作类型,每种工作都有自己的收入。正确的模式将如何寻找这一点,这样就可以为员工添加具有自己收入数据的新工作类型,并在未来对所有收入数据进行所有必要的数学运算?

Const mongoose = require(‘mongoose’)
Const Schema = mongoose.Schema

Const employeeSchema = new schema({

   employeeName: {type: string},
   job: [
          {
            jobName:{type: string},
            jobIncome:{type: number}
            date: {type: Date, default : Date.now}
          }
        ]

})

该架构看起来正确吗?

【问题讨论】:

    标签: mongodb mongoose database-design mongoose-schema


    【解决方案1】:
    Const mongoose = require(‘mongoose’)
    Const Schema = mongoose.Schema
    
    Const employeeSchema = new schema({
    
       employeeName: {type: string},
       job: [
              {
                reference:{ type: Schema.Types.ObjectId,
                    ref: "Job",
                    required: true},
                joiningDate: {type: Date, default : Date.now},
                lastDate: {type: Date}
              }
            ]
    })
    

    工作模式的定义 - 你也可以有其他字段

    Const jobSchema = new schema({
        title:{ type:string},
        income:{type:number}
    })
    module.exports = Job = mongoose.model("Job", jobSchema);
    

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多