MongoDB - 无法更新阵列

时间:2017-09-24 14:45:49

标签: javascript node.js mongodb mongoose

嗨,有人可以解释我的控制器代码有什么问题吗?我正在尝试使用控制器代码修改数组中存在的项目。它既不会出错也不会更新我的数据库

模型

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

            });

1 个答案:

答案 0 :(得分:0)

尝试使用返回更新文档的方法findOneAndUpdate

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

            });