Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB : Nested array update issue

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

   });


});

like image 450
prakash selvam Avatar asked Nov 09 '22 14:11

prakash selvam


1 Answers

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

like image 176
prakash selvam Avatar answered Nov 15 '22 07:11

prakash selvam