Justin R.
Justin R.

Reputation: 995

How do I change a field name in a collection in mongodb

{
"_id" : ObjectId("531df10dc044701691c55acf"),
"name" : "jrose",
"phonenumbers" : [
    {
        "type" : "home",
        "value" : "7707446895"
    },
    {
        "type" : "work",
        "value" : "7707336895"
    }],
"callerId" : ["7707336895"],
"PIN" : "7707336895",
"useSMS" : true
}

I want to change the "value" field to "phonenumber" inside of the "phonenumbers" array

Upvotes: 1

Views: 130

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

Seems that $rename operator doesn't work with arrays (at the moment).

> db.foo.update({}, {$rename: {'phonenumbers.value': 'phonenumbers.phonenumber'}})
$rename source field invalid

So your only choice is to read the document, modify it in the application and rewrite it in the database.

Upvotes: 1

Related Questions