Julian Gómara
Julian Gómara

Reputation: 31

Rename child in MongoDb PHP

I'm trying to rename a child in mongodb from the PHP Library (https://docs.mongodb.com/php-library/current/tutorial/)

My document structure is

{
"_id" : ObjectId("5a57c33f2ae9aa144244fa84"),
"personalizados" : {
    "Moto" : "Ducati",
    "Auto1" : "Fitito"
},
"id_mysql" : 8,
"nombre" : "Julián Gó"
}

And I want to rename "Moto" or "Auto1" (childs of personalizados)

I'm trying with:

$mongo_clientes->updateMany(
    [], 
    ['$rename' => 
        [array('personalizados.' . $campo_actual['etiqueta']) => array('personalizados.' . $etiqueta)]
    ]
);

Where $campo_actual['etiqueta'] is the old name and $etiqueta is the new, but it does not work... Do you know what is wrong?

EDIT: I getting this error:

Uncaught MongoDB\Driver\Exception\InvalidArgumentException: document to insert contains invalid key: empty key

pd: $mongo_clientes is the collection and is working fine, it works with another things like inserts

Thanks

Upvotes: 1

Views: 94

Answers (1)

Julian Gómara
Julian Gómara

Reputation: 31

The correct way to do this is:

$mongo_clientes->updateMany([], ['$rename' => ['personalizados.' . $campo_actual['etiqueta'] => 'personalizados.' . $etiqueta]]);

Upvotes: 1

Related Questions