Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为对象数组定义mongoose模式?_Mongoose_Mongoose Schema - Fatal编程技术网

如何为对象数组定义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作为图像字段来保存同一文件的多张照片。请用代码详细说明您的答案好吗?