简体   繁体   中英

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. I guess I should only put accesstokens in reference and not posts. 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. 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.

tldr;

Posts should have their own schema

Access tokens should be in user schema

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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