Adding an array inside my Mongoose schema

I have the following schema where I am trying to add an array of comments to my blog post schema, and then in the comments schema, I need to add an array of image urls associated with each specific comment. I searched the net and found this link attached documents in mongoose documentation but noticed that it is related to version 2.7 of mongoose and we are now version 3.8. So was wondering if I am doing this right? And if not someone please help me by suggesting a better way to design my blog post schema to include many blog comments as well as an array of images associated with each a comment. Thanks for your time and effort.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var pictures = new Schema({
    picURL: String,
    date: {
        type: Date,
        default: Date.now
    }
});

var comments = new Schema({
    subject: String,
    body: String,
    date: {
        type: Date,
        default: Date.now
    },
    pictures:[pictures]
});

var blogpost = new Schema({
    title: String,
    body: String,
    date: {
        type: Date,
        default: Date.now
    },
    comments:[comments]
});

module.exports = mongoose.model('BlogPost', blogpost);

      

+3


source to share


1 answer


you have two general scenarios for how you would like to process your information

Inline document:

if you are likely to do more reads than writes that it is recommended to follow this approach, in which case your model could be like this:

var comments = new Schema({
    subject: String,
    body: String,
    date: {
        type: Date,
        default: Date.now
    },
    pictures:[{
        picURL: String,
        date: {
            type: Date,
            default: Date.now
        }
    }]
});

      

and also your approach to me is fine and should work on 3.8 without any possible problems.

Link:

if you have more records than reads, you can use different collections to separate information and reference your object. For example:

var comments = new Schema({
    subject: String,
    body: String,
    date: {
        type: Date,
        default: Date.now
    },
    pictures: [
            {type: Schema.Types.ObjectId, ref: 'pictures'}
    ]
});

      



you will need to split each circuit into it and ask the model for comments and images.

Either way, both are valid if you ask me my personal preference is the embedded document approach.

EDIT:

this situation can be applied for N relationships between collections, I keep it simple for two relationships, but for you the scenario might be like this:

var blogpost = new Schema({
    title: String,
    body: String,
    date: {
        type: Date,
        default: Date.now
    },
    comments: [{
        subject: String,
        body: String,
        date: {
            type: Date,
            default: Date.now
        },
        pictures:[{
            picURL: String,
            date: {
                type: Date,
                default: Date.now
            }
        }]
    }]
});

      

link

var blogpost = new Schema({
    title: String,
    body: String,
    date: {
        type: Date,
        default: Date.now
    },
    comments:type: Schema.Types.ObjectId, ref: 'comments'}
});

      

hope this helps.

+5


source