3

The first answer google returned is: How do I remove a field completely from Mongo?

And the documentation: https://docs.mongodb.com/manual/reference/operator/update/unset/

I tried:

db.amazon_rev.update({}, {$unset: {'meta.asin': true}}, false, true)
db.amazon_rev.update({}, {$unset: {'meta.asin':1}}, false, true)
db.amazon_rev.update({}, {$unset: {'meta.asin': ''}}, false, true)
db.amazon_rev.update({}, {$unset: {'meta.asin': ''}}, {multi: true})
db.amazon_rev.update({}, {$unset: {'meta.asin':1}}, {multi: true})
db.amazon_rev.update({}, {$unset: {'meta.asin': true}}, {multi: true})

Every time it says:

WriteResult({ "nMatched" : 237199, "nUpserted" : 0, "nModified" : 0 })

and nothing changed:

(This collection was merged together from two collections (review and meta, both indexed by asin) by mongodb aggregation $lookup)

db.amazon_rev.findOne()
{
    "_id" : ObjectId("57f21916672286a104572738"),
    "reviewerID" : "A2E2I6B878????",
    "asin" : "B000GI????",
    "reviewerName" : "Big????",
    "helpful" : [
        0,
        0
    ],
    "unixReviewTime" : 137???????,
    "reviewText" : "........................................",
    "overall" : 5,
    "reviewTime" : "????, 2013",
    "summary" : "............",
    "meta" : [
        {
            "_id" : ObjectId("57f218e2672286a10456af7d"),
            "asin" : "B000GI????",
            "categories" : [
                [
                    ".................."
                ]
            ]
        }
    ]
}

Is there something I missed? Thanks for any suggestion!

Community
  • 1
  • 1
leoce
  • 715
  • 1
  • 8
  • 24
  • 1
    Possible duplicate of [Remove a field from array element in mongodb](http://stackoverflow.com/questions/19945924/remove-a-field-from-array-element-in-mongodb). Unset doesn't work on arrays. – Robert Moskal Oct 04 '16 at 03:50
  • @RobertMoskal thanks for your directing, the example in the documentation says "The following update() operation uses the $unset operator to remove the fields quantity and instock from the first document in the products collection". so I thought the `multi` would not only modify the first document. If $unset doesn't work on arrays, what kind of collection does it support? – leoce Oct 04 '16 at 04:08

1 Answers1

3

Can try using positional operator $ and check that field exist or not using $exists. this process only unset the first element of an array.

db.amazon_rev.update({
   "meta.asin": {$exists: true}
},{
   $unset: {
      "meta.$.asin" : true
   }
},false,true);

if you want to delete each asin from meta array then better process is find documents and remove each meta.asin then save again that document.

Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68