0

MongoDB user entry

enter image description here

Controllers code:

enter image description here

                module.exports.deleteIncome = (params) => {
                    return User.findById(params.userId) // I went to the user here
                    .then((user, err) => {
                        if(err) return false
                        user.incomeRecords.remove( "_id": "{params.incomeRecordId}" ) // my delete income record syntax
                        return user.save()
                        .then((updateUser, err) => {
                            return(err) ? false : true
                        })
                    })
                }

Here's my code, it's not working
Please see the pic i provided
I want to delete the specific income record inside the user's array incomeRecords
Please help thanks =)

Manoj
  • 2,059
  • 3
  • 12
  • 24
  • 1
    Does this answer your question? [How to remove array element in mongodb?](https://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb) – eol Oct 18 '20 at 09:35

1 Answers1

0

You can use the mongodb pull operator. It would look something like this:-

findOneAndUpdate({userid:params.userid},{
  $pull:{
      incomeRecords:{
         _id:<the mongodb id here>
      }
  }
})

Check out the mongodb docs

Avichal Gupta
  • 46
  • 1
  • 5