0

I'd like to make a mongoose schema to hold several photos of each user.

I know how to define the schema for a single photo:

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);

But I'm wondering how can I generalize the schema to hold multiple photos, each of which having the same fields as above (imagId, isProfilePImg and visible)?

2
  • You can use array-json for image field to hold multiple photos for same fileds. Commented Oct 17, 2018 at 9:42
  • Can you please elaborate your answer with code? Commented Oct 17, 2018 at 9:44

1 Answer 1

1

Try this schema:

const PhotoSchema = new Schema({

  user: {
    type: Schema.Types.ObjectId,
    ref: 'users'
  },
  photos: [
    {
      imgId: {
        type: Number,
      },
      isProfileImg: {
        type: Boolean,
        default: true,
      },
      visible: {
        type: String,
      }
    }
  ]
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.