Mongo database help?

I am create a post schema and In post schema I have a replies field where I track all replies related to the post and reply field is array of object but I don’t understand how do I create schema field for array of object any help?

var postSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    description: {
        type: String,
        required: true,
        trim: true
    },
    likes: {
        type: Number
    },
//this thing, the reply field is array of object
    replies: {
        type: Object
    }

}, {
    timestamps: true
});

I think it’s done like this (with Array literal):

    replies: {
        type: [ Object ]
    }
1 Like

Hello there,

The best practice would be to define the type of object:

const schema1 = new mongoose.Schema({
  title: String,
  amount: Number,
  myObject: [schema2]
});

const schema2 = new mongoose.Schema({
  field: String,
  amount: Number,
}):

// Etc.

Hope this helps

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.