创建引用其他模式的MongoDB模式

时间:2020-01-13 14:11:10

标签: javascript node.js mongodb

我在理解如何构建模式方面遇到一些困难,希望能获得一些帮助。 简而言之,我有三种模式:公司,用户和验证。我首先创建一些公司,然后创建用户并将其添加到公司中。

然后我登录到网页,并使用一种表单向表中添加行(konto,beskrivning,projekt ...)。然后,将验证保存到MongoDB。

第一个问题是:当前方案是否适合我的方案?还是我需要改变一些东西。我输入了多余的信息吗?

第二个问题:在VerificationSchema中,我希望每个公司的递增值(verificationNumber)以1开始,然后为每个添加的验证增加。每个公司的计数器应独立,但应与同一公司的所有用户共享。

Verification.js

const VerificationSchema = mongoose.Schema({
verificationNumber: Number,
user :{type: mongoose.Schema.Types.ObjectId, ref: 'User'},
company: {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}, 
rows: [{
    konto: Number,
    beskrivning: String,
    projekt: String,
    debet: Number,
    kredit: Number
  }]
});

Company.js

const CompanySchema = mongoose.Schema({
companyName: String,  
users: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
verifications: [{type: mongoose.Schema.Types.ObjectId, ref: 'Verification'}]
});

User.js

const UserSchema = mongoose.Schema({
    userName: String,  
    company: {type: mongoose.Schema.Types.ObjectId, ref: 'Company'],
    verifications: [{type: mongoose.Schema.Types.ObjectId, ref: 'Verification'}]
    });

0 个答案:

没有答案