node.js - MongoDB : Nested array update issue -


i using mean stack display following array in grid.

nested array:

{      "_id":"1",    "appdomain":[         {            "appdomainname":"xyz",          "dashboard":[               {                  "envname":"uat",                "envdetails":[                     {                        "hostnme":"abc",                      "ip":"sdsdsdsd",                      "cpu":"-------",                      "memory":"-------",                      "disk":"-------",                      "downtime":"sdsdsdsd",                      "version":"dsdsdsd",                      "hostdetails":[                           {                              "hosthdrname":"tomcat",                            "hostdetails":[                                 {                                    "hostname":"flt",                                  "status":"up",                                  "path":"dfdf",                                  "host":"sdsdsd",                                  "port":"1112"                               }                            ]                         }                      ]                   }                ]             }          ]       },       {            "appdomainname":"abc",          "dashboard":[               {                  "envname":"uat",                "envdetails":[                     {                        "hostnme":"abc",                      "ip":"sdsdsdsd",                      "cpu":"-------",                      "memory":"-------",                      "disk":"-------",                      "downtime":"sdsdsdsd",                      "version":"dsdsdsd",                      "hostdetails":[                           {                              "hosthdrname":"tomcat",                            "hostdetails":[                                 {                                    "hostname":"flt",                                  "status":"up",                                  "path":"dfdf",                                  "host":"dfdfdf",                                  "port":"1112"                               },                               {                                    "hostname":"shp",                                  "status":"down",                                  "path":"dfdfdf",                                  "host":"fgfgfg",                                  "port":"1112"                               }                            ]                         }                      ]                   }                ]             }          ]       }    ] } 

mangoose update: using express.js add/update , delete entry in mangodb. facing issue in update in nested array. please see below code update nested array.

router.post('/appdomain/update/:appdomainname/:envname', function(req, res, next) { appdomain.findone({}, {_id: 0, appdomain: {$elemmatch: {appdomainname: req.params.appdomainname,}}}, function (err, appdomain) {      if(appdomain.appdomain[0].dashboard[0].envname == req.params.envname )             appdomain.appdomain[0].dashboard[0].envdetails.push({})       appdomain.save(function (err) {         if(err) {             console.error('error!');         }     });     res.json(appdomain);      }); }) 

however, not updating. great if can me out..

appdomainmodel schema

      var schema = mongoose.schema;      var serverdetails = new schema({         hostname: string,         ip: string,         status: string,         path: string,         host: string,         port: string     });      var hostdetail = new schema({         hosthdrname: string,         hostdetails: [serverdetails]     });      var keyvalue = new schema({         keyhdrname: string,         keyvaluedata: [{key: string, value:string}]     });      var envschema = new schema({         hostnme: string,         ip: string,         cpu: string,         memory: string,         disk: string,         downtime: string,         version: string,         hostdetails: [hostdetail],         keyvalues: [keyvalue],         activities:{             recent: [keyvalue],             planned: [keyvalue]         }     });      var dashboardschema = new schema({         envname: string,         envdetails: [envschema]     });        var appdomainschema = new schema({         _id:string,         appdomain:[{             appdomainname: {type:string,index: { unique: true }},             dashboard: [dashboardschema]         }]     });      var appdomain = mongoose.model('appdomain', appdomainschema);     var dashboard = mongoose.model('dashboard', dashboardschema);     var envschema = mongoose.model('envschema', envschema);  

after updating.i using following function check value .. updated 1 not available in db.

  router.get('/app/domain/get/:appdomainname', function(req, res, next) {       appdomain.find({}, {_id: 0, appdomain: {$elemmatch: {appdomainname: req.params.appdomainname,}}},function (err, appdomain) {         res.json(appdomain);     });   });  

after long struggle, figured out . see below answer.

      var data2 = {};      appdomain.update(       {_id:1,'appdomain.appdomainname':  req.params.appdomainname,'dashboard.$.envname':{$nin:[req.params.envname]}},             {$push: {'appdomain.$.dashboard.0.envdetails':{'envdetails':[data2]}}},             function(err, model) {                 console.log(err);             }         );  

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -