简体   繁体   English

猫鼬用户架构设计和参考文章列表

[英]Mongoose User Schema design and array of posts In ref

I Have schema design as below. 我的架构设计如下。 I have posts array which is reference to the post model. 我有职位数组,这是对职位模型的引用。 Is it good idea to put it in User schema or should I not include as it is always growing as users add their post. 将它放在User模式中是个好主意,还是不应该包含它,因为随着用户添加其帖子,它一直在增长。 I guess I should only put accesstokens in reference and not posts. 我想我应该只将accesstokens放在参考中而不是帖子中。 Am I thinking right? 我在想对吗?

var UserSchema = new Schema({
  username: {
    type: String,
    unique: true,
    required: true,
    lowercase: true,
    trim: true
  },
  encrypted_password: {
    type: String,
    required: true
  },
  salt: {
    type: String,
    required: true
  },
  email: {
    type: String,
    unique: true,
    required: true,
    lowercase: true,
    trim: true
  },
  mobile: {
    type: Number,
    unique: true
  },
  bio: {
    type: String
  },
  created: {
    type: Date,
    default: Date.now
  },
  access_tokens: [{type: Schema.Types.ObjectId, ref: 'AccessToken'}],
  posts: [{type: Schema.Types.ObjectId, ref: 'Post'}]
}, { collection: 'users' });

You should have a separate collection for Posts but you should keep the access_tokens within the user schema. 您应该为Posts提供一个单独的集合,但是应该将access_tokens保留在用户架构中。 One good reason you might consider separating the posts into its own collection is there are many use cases where you will query for just posts. 您可能会考虑将帖子分为自己的集合的一个很好的理由是,在许多用例中,您将只查询帖子。 However, with access_tokens , they will always be tied to a user. 但是,使用access_tokens ,它们将始终与用户绑定。

tldr; tldr;

Posts should have their own schema 帖子应该有自己的模式

Access tokens should be in user schema 访问令牌应位于用户架构中

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM