PHP MongoDB update multiple documents using $in/$or
Asked Answered
B

1

5

I'm having trouble figuring out how to construct an update query in PHP that will update document IDs X,Y, and Z. Does anyone have experience with this?

$ids[] = array(
  new MongoId('4eaaf929498fe2c80300000c'),
  new MongoId('4eaaff24498fe2ba0900001f')
); 
$collection->update(
  array('_id' => array('$in' => $ids)),
  array('$set' => array("title"=>"test")),
  array("upsert" => true)
);
Bride answered 28/10, 2011 at 19:49 Comment(2)
Update document IDs or update based on document IDs? If the latter, update what exactly? Can you show some code?Foreside
So I want to update the field "title" on document IDs X,Y and Z. Here is my current code: $ids[] = array(new MongoId('4eaaf929498fe2c80300000c'), new MongoId('4eaaff24498fe2ba0900001f')); $collection->update(array('_id' => array('$in' => $ids)), array('$set' => array("title"=>"test")), array("upsert" => true));Bride
I
10

I assume that your trouble is, that only one document get's updated, right? If this is the case, there is another setting you have to specifiy in the third parameter (the options parameter) for the update method:

multiple => true

Otherwise, it will only update the first match of your query. See the documentation to the update method at:

http://de.php.net/manual/en/mongocollection.update.php

Inerney answered 7/11, 2011 at 13:28 Comment(1)
Yes, the PHP Driver is using a different parameter name. In mongo default api, the option is called only multiRespirable

© 2022 - 2024 — McMap. All rights reserved.