4

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!

2
  • 1
    Possible duplicate of Remove a field from array element in mongodb. Unset doesn't work on arrays. Commented Oct 4, 2016 at 3: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? Commented Oct 4, 2016 at 4:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.