Adam Magaluk
Adam Magaluk

Reputation: 1726

Unset an array element

Is it possible to use the $unset operator on array field and remove an element that matches a query. For example i'm trying to remove 35 from field "files" array.

{
  _id : 1,
  files : [1,12,35,223]
}
// Ive tried this but it does not work
db.col.update({_id : 1}, {$unset : { files : 35}})
// or this does not work
db.col.update({_id : 1}, {$unset : { "files.35" : 1}})

Upvotes: 0

Views: 114

Answers (2)

user20140268
user20140268

Reputation: 1288

You can use $pull instead:

db.col.update({ _id : 1 }, {$pull: { files : 35 } })

Upvotes: 0

Ryan Artecona
Ryan Artecona

Reputation: 6203

Have you tried the $pull operator? As in:

db.col.update({_id: 1}, {$pull: {files: 35}})

Upvotes: 2

Related Questions