如何构造同时具有一对多和多对五关系的猫鼬文档?

时间:2018-10-05 19:37:34

标签: node.js mongoose database-design entity-relationship

我试图用猫鼬设计数据模型,但是我对构造文档的最佳方法感到困惑。

Entity Relationship Diagram (ERD) image

  • 用户可以创建公司,职位和简历(一对多)
  • 公司有工作(一对多)
  • 用户可以申请工作(多对多)
  • 工作可以有简历(多对多)

公司与Job有一对多关系,而用户与Job有多对多关系。敬畏约伯更好吗?

是否有更好的文档结构方式?

这是我第一次使用mongoose和Node.js。任何帮助将不胜感激

User.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Define collection and schema for User
var User = new Schema({
    companies: [{
        jobs: [{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Job'
        }]
    }],
    applied_jobs: [{
         type: mongoose.Schema.Types.ObjectId,
         ref: 'Job'
    }],
    resumes: [{ 
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Resume'
    }]
}, {
        collection: 'users'
    });

module.exports = mongoose.model('User', User);

Job.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Define collection and schema for Job
var Job = new Schema({
    created_by: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    submitted_resumes: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Resume'
    }]
}, {
        collection: 'jobs'
    });

module.exports = mongoose.model('Job', Job);

Resume.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Define collection and schema for Resume
var Resume = new Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }
}, {
    collection: 'resumes'
  });

module.exports = mongoose.model('Resume', Resume);

0 个答案:

没有答案