【发布时间】:2020-10-29 14:13:37
【问题描述】:
我有一个名为“书签”的猫鼬模式字段,我希望它引用多个模型。例如,如果我有 2 个名为“post”和“complain”的其他模型。我希望用户能够为他们添加书签。
const userSchema = new mongoose.Schema({
fullname: {
type: String,
required: true,
trim: true,
lowercase: true
},
username: {
type: String,
unique: true,
required: true,
trim: true,
lowercase: true
},
email: {
type: String,
unique: true,
required: true,
trim: true,
lowercase: true,
validate(value) {
if (!validator.isEmail(value)) {
throw new Error('Email is invalid')
}
}
},
bookmarks: [{
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Post'
}],
})
下面是一个帖子模型,用户可以在其中发布一般内容
const postSchema = new mongoose.Schema({
body: {
type: String,
required: true,
trim: true,
lowercase: true
}
})
下面是一个投诉模型,用户可以在其中发布投诉
const complainSchema = new mongoose.Schema({
body: {
type: String,
required: true,
trim: true,
lowercase: true
}
})
如何获取用户模型中的书签字段,以便能够同时获取投诉模型和帖子模型的对象 ID?
【问题讨论】:
标签: javascript node.js mongoose