Push element to array in mongoose

前端 未结 4 498
逝去的感伤
逝去的感伤 2021-01-20 07:42

I am trying to push an element to an array in mongoose. I am doing it with update and $push. But it is not updating it in the database. This is my code. routes.js:



        
4条回答
  •  无人共我
    2021-01-20 08:16

    We can do like that

    Model.findOneAndUpdate({"_id":req.body.id},{
                "$push": {"resources": req.body.resources}
            },{new: true, safe: true, upsert: true }).then((result) => {
                return res.status(201).json({
                    status: "Success",
                    message: "Resources Are Created Successfully",
                    data: result
                });
            }).catch((error) => {
                return res.status(500).json({
                    status: "Failed",
                    message: "Database Error",
                    data: error
                });
            });`
    

提交回复
热议问题