Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - Unset an attribute from a single array element

Tags:

mongodb

How can I unset an attribute from a single array element from the Mongo console. For example, how do I unset the junk attribute from time[1]

{
  "_id" : ObjectId("4d525ab2924f0000000022ad"), 
  "name" : "hello", 
  "time" : [
      {
          "stamp" : "2010-07-01T12:01:03.75+02:00",
          "reason" : "new"
      },
      {
          "stamp" : "2010-07-02T16:03:48.187+03:00",
          "reason" : "update",
          "junk"  : "yes"
      },
      {
          "stamp" : "2010-07-02T16:03:48.187+04:00",
          "reason" : "update"
      },

   ]
}
like image 569
rdsoze Avatar asked Jan 16 '12 11:01

rdsoze


1 Answers

This should do the trick:

db.coll.update({"time.junk": "yes"}, {$unset: {"time.$.junk": 1}});

Read on dot notation.

like image 191
Sergio Tulentsev Avatar answered Dec 16 '22 21:12

Sergio Tulentsev