5
votes

I am using MEAN stack to display the following array in a 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: I am using express.js to add/update and delete the entry in MangoDB. I am facing the issue in update in nested array. Please see below the code for update the 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, it is not updating. It would be great if anyone can help 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 am using the following function to check the value .. However the updated one is not available in the 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);

   });


});

2
Can you add Schema that you use for AppDomain Model?vanadium23
@vanadium23:added the AppDomain Model. Please check.prakash selvam
Are you sure it is not updating? If you query your database do you only find one unchanged document? To get the result you are looking for, you may just need to pass another parameter to your callback in save and then call res.json on that parameter from inside the callback.Dan J Miller
@Dan:Yes,After updating.I am using the following function to check the value .. However the updated one is not available in the DB. Please check my questions blockprakash selvam

2 Answers

1
votes

After a long struggle, I figured it out . See below the 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);
            }
        );

0
votes

I suspect your changes are actually being saved successfully, but you're returning the response too soon. Try this:

appDomain.save(function (err, savedAppDomain) {
    if(err) {
        console.error('ERROR!');
        next(err);
        return;
    }
    res.json(savedAppDomain):

});

// Removed:
//res.json(appDomain);
//});