Mongoose schema help

I have a collection named has a field called comments.

This comment could either be a text or image.
How can i restructure my schema to achieve this.

for uploading images i am using multer package and storing the image in filesystem.
I am thinking to store the image location in my database instead of storing image as buffer in Db.

const channelSchema = new mongoose.Schema({
    channelName: {
        type: String,
        required: true
    details: {
        type: String,
        required: true
    createdBy: {
        type: ObjectId,
        required: true,
    comments: [
            content: String,
            timeStamp: { type: Date, default: },
            createdBy: { type: ObjectId, ref: 'Users' }

You should try to format your question more appropriately. Could you stick this stuff:

Into a code tag.


That’s your answer :slight_smile:!

Have done this but the new problem is accessing the file from client.I am actually storing file path inside DB.

exports.getChannelComments = async (req, res) => {
    const {channelId}=req.params;
    Channel.findById({_id:channelId}).select('comments').populate('comments.createdBy','userName photoUrl')
        .then(comments => {
        .catch(err => console.log(err));

I am getting all the comments from this.But for every comments which have a file path do need to make multiple requests to fetch files from node server.

Since it seems that you don’t need to restrict access to the files, you could serve the images from a static route:

express.use('/static', 'public');

Then you could have a folder public/images/comments and store the files there. The path stored on the database could be images/comments/comment-id.imgextension, where comment id is just that and the imgextension would be something like png or jpg.