0

I'm trying to delete some fields on my mongodb database but instead the fields are just getting mutated to a null value and that's an issue because when fetching data im using loops and they get broken passing true null value ... for deleting data i'm using the following :

 User.updateOne({_id : user._id } ,{ $unset :{ "days.0.tasks.0" = "" })

what i'm getting :

image

Is there a way to delete entirely the objects inside the tasks array?

Caconde
  • 4,177
  • 7
  • 35
  • 32
user121548789
  • 189
  • 1
  • 9

1 Answers1

0

$unset is not working as you'd expect it because you are using it on an element inside an array. Removing the element at position 0 means shifting all other elements in the array. To prevent messing with those other elements, $unset is setting the element at position 0 to null.

MongoDB docs

I suggest using the $pull operator to delete specific elements from an array or use $pop for the first / last items .

OR :

you can get the user tasks array and splice it then push it back using $set operator.

user121548789
  • 189
  • 1
  • 9
Félix Paradis
  • 5,165
  • 6
  • 40
  • 49