1

I want to make this type of array in mongodb

dishes: {dish1, dish2},

there should be multiple dishes in the dishes not just one so output looks like

"dishes":{ //dish one object, //dish two object, }

how do i do that. i am using mongodb as database and nodejs as server.

1 Answers1

2
const yourSchema = new Schema({
    //Other properties,
    dishes: {
        type: [{
            dishName: {type: String},
            dishId: {type: String},
            //OtherProperties of dish object
        }],
        default: []
    },
    //Other properties
});

You can define your array of object like this in your schema. I hope you are using Mongoose. For more information refer Documentation and this answer.

Your data will be formated like this:

dishes: [
    {dishName: "dish1", dishId: "1", ... },
    {dishName: "dish2", dishId: "2", ... },
    .
    .
    .
]
Nilupul Sandeepa
  • 748
  • 6
  • 20