Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1014

A modifier to delete a single value from an array

    • Query

      When dealing with arrays with non unique values, it is impossible to remove a single value from that array in 1 update.

      $pop doesn't work, because the value can be anywhere in the array.
      $pull doesn't work, because that will remove all occurences of the value (which can happen in a non unique array)
      $unset doesn't work, because that sets the value to NULL, instead of removing it.

      EG:

      > db.example.remove()
      > db.example.insert({_id:1, sequence : [1,2,3,4,3,2,3,4]})
      > db.example.update({_id:1}, { $unset : { 'sequence.1' : 1 }})
      > db.example.find()

      { "_id" : 1, "sequence" : [ 1, null, 3, 4, 3, 2, 3, 4 ] }

      I'm looking for a modifier that leaves me with

      { "_id" : 1, "sequence" : [ 1, 3, 4, 3, 2, 3, 4 ] }

      Maybe something like

      > db.example.update({_id:1}, { $remove : { 'sequence.1' : 1 }})

      I'm curious what you think of it. Currently, with $addToSet and $pull it's very easy to manage arrays with unique values. For non unique arrays, there's $push to add values, but no proper way to remove values.

            Created:
            Updated:
            Resolved: