如何为对象数组定义mongoose模式?
我想制作一个猫鼬模式来保存每个用户的几张照片 我知道如何为一张照片定义模式:如何为对象数组定义mongoose模式?,mongoose,mongoose-schema,Mongoose,Mongoose Schema,我想制作一个猫鼬模式来保存每个用户的几张照片 我知道如何为一张照片定义模式: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const PhotoSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: 'users' }, imgId :{ type: N
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PhotoSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
imgId :{
type: Number,
}
isProfileImg: {
type: Boolean,
default: true,
},
visible: {
type: String,
},
});
module.exports = Photo = mongoose.model('Photo', PhotoSchema);
但我想知道如何将模式概括为包含多张照片,每个照片都具有与上面相同的字段imagId、isProfilePImg和visible?尝试以下模式:
const PhotoSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
photos: [
{
imgId: {
type: Number,
},
isProfileImg: {
type: Boolean,
default: true,
},
visible: {
type: String,
}
}
]
});
您可以使用数组json作为图像字段来保存同一文件的多张照片。请用代码详细说明您的答案好吗?