A. Duff
A. Duff

Reputation: 4309

Renaming fields in every document in a MongoDB collection using PHP

Following this question I gather that upsert: false and multi: true fields need to be set for this to work.

However, when I try to code this in PHP, I have a problem:

$conn = new Mongo("mongodb://foo:bar@localhost:27017");
$db = $conn->selectDB("someDB");

$data = array('$rename' => array(
  'nmae' => 'name'
));
$db->command(array(
  'findAndModify' => 'foo',
  'update' => $data,
  'upsert' => 'false',
  'multi' => 'true'
));

After running this script, only the first document with the nmae typo is changed to name; the rest still say nmae. The same as if I had run it without the upsert and multi options.

I also tried this:

$data = array('$rename' => array(
  'nmae' => 'name'
),
'upsert' => 'false',
'multi' => 'true'
);
$db->command(array(
  'findAndModify' => 'foo',
  'update' => $data
));

But that does the same thing.

Any way to get this working?

Upvotes: 0

Views: 873

Answers (1)

unkhan
unkhan

Reputation: 278

The findAndModify query doesn't have a "multi" option: http://www.php.net/manual/en/mongocollection.findandmodify.php

What you probably want to use is update instead: http://www.php.net/manual/en/mongocollection.update.php

Upvotes: 2

Related Questions