Sona Shetty Sona Shetty - 3 years ago 210
Node.js Question

MongoDB - Unable to update array

Hi Can some one explain what's wrong with my controller code?I am trying to modify an item present in an array using the controller code.It neither gives error nor update my db

Model

var localTransportSchema = new Schema({

name: { type: String, required: false, trim: true },
contact: {
addressLine1: { type: String, required: false },
serviceActiveFlag: { type: String, required: false, enum: ['Y', 'N'] },
},
vehicle: [{
vehicleType:{ type: String, required: false, enum: ['sedan', 'hatchback', 'suv', 'mpv', 'luxury'] },
activeFlag:{type: String, required: false, enum: ['Y', 'N']}
}]
});
module.exports.transportModel = mongoose.model(collection, localTransportSchema);


Controller

transportModel.findOne({ "name": req.body['providerName'], "contact.postalCode": parseInt(req.body['postalCode']), "vehicle": { $elemMatch: { "vehicleType": req.body['vehicleType'] } } },
function (err, doc) {
if (err) {
logger.error("Error while updating record : - " + err.message);
return reject(res.status(409).json({
"Message": "Error while updating transport details for provider " + req.body['providerName'] + " in transport details table"
}));
} else if (doc === null) {
logger.error("Error while updating record in transport details : - unable to update database");
return reject(res.status(409).json({
"Message": "Error while updating transport details for provider " + req.body['providerName'] + " due to " + err.message
}));
} else {

doc.set({
"vehicle.$.activeFlag": req.body['vehicleActiveFlag']
})
//save document
doc.save()
}

});

Answer Source

Try to use a method findOneAndUpdate which return updated document:

transportModel.findOneAndUpdate({ "name": req.body['providerName'], "contact.postalCode": parseInt(req.body['postalCode']), "vehicle": { $elemMatch: { "vehicleType": req.body['vehicleType'] } } }, {$set: {"vehicle.$.activeFlag": req.body['vehicleActiveFlag']}}, {new: true},
             function (err, doc) {
                if (err) {
                    logger.error("Error while updating record : - " + err.message);
                    return reject(res.status(409).json({
                        "Message": "Error while updating transport details for provider " + req.body['providerName'] + " in transport details table"
                    }));
                } else if (doc === null) {
                    logger.error("Error while updating record in transport details : - unable to update database");
                    return reject(res.status(409).json({
                        "Message": "Error while updating transport details for provider " + req.body['providerName'] + " due to " + err.message
                    }));
                } else {
                      console.log(doc);
                      res.status(200).json(doc); 
                  }

            });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download