嵌套对象数组更新mongodb

时间:2018-07-23 06:02:31

标签: node.js mongodb express mongoose

我正在尝试更新嵌套数组中的值,但无法使其正常工作。

我的对象就是这样

                    User.findByIdAndUpdate({ _id : userId, 'vehicle._id' : vehicleId },{ $push : { reg_number : reg_number,
                  category : category, model_year: model_year, insurance_date:insurance_date, tax_date:tax_date,
                  vehicle_name:vehicle_name }}, function(err, vehicle){

                if(err) { throw err }
                req.flash('success','Updated');

                //res.render('dashboard/vehicleUpdate', {vehicle});
              });

Mongodb如下所示:

DB

1 个答案:

答案 0 :(得分:0)

尝试此操作,同时检查Update Values in an Array

User.updateOne({ _id : userId, "vehicle._id" : vehicleId },
{ $set: { "vehicle.$.reg_number" : reg_number }}, function(err, vehicle)
{
       if(err) { throw err }
                req.flash('success','Updated');

            //res.render('dashboard/vehicleUpdate', {vehicle});
              });