2017-09-24 15 views
0

こんにちはを更新することができませんいくつかのいずれかが、私はcode.Itがエラーを与えないも私のデシベルのMongoDB - 配列

モデルを更新し、どちらもコントローラを使用して配列内のアイテムの存在を変更しようとしています私のコントローラのコードが悪いのかを説明することはできますか?

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

コントローラ

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

      }); 

答えて

0

更新されたドキュメントを返すメソッドfindOneAndUpdate使用してみてください:

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

      }); 
+0

awesome.Thanks your help –