Node.js 带有其他模式数组字段的Mongoose模式

Node.js 带有其他模式数组字段的Mongoose模式,node.js,mongoose,mongoose-schema,Node.js,Mongoose,Mongoose Schema,首先,我是一名初学者,目前正在开发一种社交媒体博客类型。 现在,我有了userSchema和postSchema模型: 用户模型 const userSchema = new mongoose.Schema({ name: { type: String, required: [true, 'Please insert your name'], }, email: { type: String, required: [true, 'Please inse

首先,我是一名初学者,目前正在开发一种社交媒体博客类型。 现在,我有了userSchemapostSchema模型:

用户模型

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'Please insert your name'],
  },
  email: {
    type: String,
    required: [true, 'Please insert your email'],
    unique: true,
    lowercase: true, //transform into lowercase / not validator
    validate: [validator.isEmail, 'Please provide a valid email'],
  },
  avatar: {
    type: String,
  },
  role: {
    type: String,
    enum: ['user', 'admin'],
    default: 'user',
  },
  password: {
    type: String,
    required: [true, 'Please provide a password'],
    minLength: 8,
    select: false,
  },
  passwordConfirm: {
    type: String,
    required: [true, 'Please confirm your password'],
    validate: {
      validator: function (el) {
        return el === this.password;
      },
      message: 'Passwords are not the same',
    },
  },
  passwordChangedAt: Date,
  posts: [] // ???????????????
});
const postSchema = new mongoose.Schema({
  title: {
    type: String,
    required: [true, 'A post must have a title'],
  },
  author: {
    type: String,
    required: [true, 'A post must have a title'],
  },
  likes: {
    type: Number,
    default: 10,
  },
  comments: {
    type: [String],
  },
  image: {
    type: String,
  },
  postBody: {
    type: String,
    required: [true, 'A post must contain a body'],
  },
  createdAt: {
    type: Date,
    default: Date.now(),
    select: false,
  },
});
后期模型

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'Please insert your name'],
  },
  email: {
    type: String,
    required: [true, 'Please insert your email'],
    unique: true,
    lowercase: true, //transform into lowercase / not validator
    validate: [validator.isEmail, 'Please provide a valid email'],
  },
  avatar: {
    type: String,
  },
  role: {
    type: String,
    enum: ['user', 'admin'],
    default: 'user',
  },
  password: {
    type: String,
    required: [true, 'Please provide a password'],
    minLength: 8,
    select: false,
  },
  passwordConfirm: {
    type: String,
    required: [true, 'Please confirm your password'],
    validate: {
      validator: function (el) {
        return el === this.password;
      },
      message: 'Passwords are not the same',
    },
  },
  passwordChangedAt: Date,
  posts: [] // ???????????????
});
const postSchema = new mongoose.Schema({
  title: {
    type: String,
    required: [true, 'A post must have a title'],
  },
  author: {
    type: String,
    required: [true, 'A post must have a title'],
  },
  likes: {
    type: Number,
    default: 10,
  },
  comments: {
    type: [String],
  },
  image: {
    type: String,
  },
  postBody: {
    type: String,
    required: [true, 'A post must contain a body'],
  },
  createdAt: {
    type: Date,
    default: Date.now(),
    select: false,
  },
});
现在我不知道这是否是最好的方法,但我想在userSchema中有一个字段,类型为postSchema数组,因此我将为每个用户创建他们自己的帖子。我可以这样做吗?如果不是,我如何才能做到? 我应该使用搜索参数字段按作者筛选帖子吗?我真的很困惑我应该如何处理这种情况。谢谢大家

看看这个例子

constuserschema=newmongoose.Schema({
用户名:String,
职位:[{
类型:mongoose.Schema.Types.ObjectId,
参考:“邮政”
}]
})
const PostSchema=新的mongoose.Schema({
内容:字符串,
作者:{
类型:mongoose.Schema.Types.ObjectId,
参考:“用户”
}
})
const Post=mongoose.model('Post',PostSchema',posts');
const User=mongoose.model('User',UserSchema,'users');
module.exports={User,Post};
信用证:

查看此示例

constuserschema=newmongoose.Schema({
用户名:String,
职位:[{
类型:mongoose.Schema.Types.ObjectId,
参考:“邮政”
}]
})
const PostSchema=新的mongoose.Schema({
内容:字符串,
作者:{
类型:mongoose.Schema.Types.ObjectId,
参考:“用户”
}
})
const Post=mongoose.model('Post',PostSchema',posts');
const User=mongoose.model('User',UserSchema,'users');
module.exports={User,Post};

信用证:

谢谢,我会尝试在我的项目中实施它。Adrian成功了吗?是的,这正是我想要的。现在我正在学习更多关于Mongo DB中的关系。谢谢你,很高兴帮助@Adrian!你能接受我的回答吗?谢谢对不起,我现在接受了。我对这整件事都是新手谢谢你,我会尝试在我的项目中实现它的@Adrian?是的,这正是我想要的。现在我正在学习更多关于Mongo DB中的关系。谢谢你,很高兴帮助@Adrian!你能接受我的回答吗?谢谢对不起,我现在接受了。这整件事我都是新手