5

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);

   });


});

4
  • Can you add Schema that you use for AppDomain Model? Commented Apr 29, 2015 at 12:18
  • @vanadium23:added the AppDomain Model. Please check. Commented Apr 29, 2015 at 13:04
  • 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. Commented Apr 29, 2015 at 15:46
  • @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 block Commented Apr 30, 2015 at 6:42

2 Answers 2

1

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);
            }
        );

Sign up to request clarification or add additional context in comments.

Comments

0

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);
//});

1 Comment

: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 block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.