Push element to array in mongoose

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

    As far as I know, you have to do as below.

    Model.findAndUpdate({_id: 'your_id'}, 
                        {$push: {'your_array_field': 
                        {"name": "foo","idAccount": 123456}}}, 
                        {new: true}, (err, result) => {
                        // Rest of the action goes here
                       })
    

提交回复
热议问题