通过Node.js和MongoDB中的ID更新对象

时间:2020-10-26 19:47:19

标签: node.js mongodb

这是我的控制器代码 我想通过其ID更新地址对象,而我的mongodb集合名称是具有数组对象地址的用户。

This is my user model details where I want to update the object by it's id

exports.updateAddressObject = (req, res) => {
let address = []
address.push({
    fullname: req.body.fullname,
    country: req.body.country,
    streetnumber: req.body.streetnumber,
    apartment: req.body.apartment,
    city: req.body.city,
    region: req.body.region,
    pincode: req.body.pincode,
    phone: req.body.phone,
    landmark: req.body.landmark
})
User.findOne({'_id': req.params.userId,'address': {'_id':req.params.addressId}},
    { $push: { address: address } },
    { 'new': true },
    (error, data) => {
        if (error) {
            return res.status(400).json({
                error: "Could not update user's new Address"
            });
        }
        res.json(data)
    }
)
};

0 个答案:

没有答案