Push element to array in mongoose

前端 未结 4 534
逝去的感伤
逝去的感伤 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:07

    This is how you can update a model in mongoose (using async await) :

    
    let updatedModel = await Model.findByIdAndUpdate(req.params.id,
    
     { $push: { accounts:{"name": "foo", "idAccount": 123456} } },
    
     { 'upsert': true });
    

    or in your code

    Maybe you have missed the {new: true, upsert: true } if you want the updated document to be returned to you.

    Chooser.update({_id: req.params.id}, {$push: {accounts: {"name": "foo", "idAccount": 123456}}},{new: true, upsert: true });
    
    

提交回复
热议问题