Sunday, 16 April 2017

How do I update the values in the mongodb nested array?

I am building a nodejs app, that stores the menus of the different restaurants. The schema is given below -

  var company = new Schema({
    company_name : String,
    categories : [category]
});

  var category = new Schema({
    // id : Object,
    category_name : String,
    subCategories : [subCategory]
});

var subCategory = new Schema({
    subCategory_name : String,
    items : [item]

});

var item = new Schema({
    item : String,
    price : Number,
    currency : String

});
  
module.exports = mongoose.model('Menu', company)

I have a rout in node app for example :

app.post("/:_company_id/:_category_id/:_sub_cat_id/", function(res,req){...})

using above method I want to insert/update the items so how do i do that.

Also while doing research I found that the mongoose does not support the nested array update. by using positional approach $ example categories.$.subCategories.$.items Cannot use this because mongoose does not support this.

Please help me with some other trick/hack which may help me in updating it. Thank you. If not than I have to move to some relational databases.



via Sandeep Kumar