Reputation: 658
I'm trying to update multiple items in a collection using the update() function with multi=true param on mongolab platform. The problem is only the first item in the collection is updated.
The collection:
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "first"
},
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "second"
}
Script code:
$db->collection->update(
array('value' => '1234'),
array('$set' => array(
'name' => 'example name',
)),
array('multi' => true)
);
Result:
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "example name"
},
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "second"
}
The update() function accepts only three arrays as arguments.
Upvotes: 0
Views: 1137
Reputation: 383
Hi Yogesh,
I think your query should be:
db.collectionName.update({"value":"1234"},{$set:{"name":"example name"}},{'upsert':true, 'multiple': true});
The corresponding PHP codes would be:
<?php
$m = new MongoClient(); //connects to local mongo
$db_name = 'ri'; //replace database name you want to work with
$collection_name = 'brand_graph'; //replace collection name you want to work with
$select = $m->selectDB($db_name)->selectCollection($collection_name);
$where_array = array(
'value' => 1234
);
$update_data = array(
'$set' => array(
'name' => 'example name'
)
);
$options = array(
'upsert' => true,
'multiple' => true
);
$select->update($where_array, $update_data, $options);
?>
I hope this is what your were looking for.
Upvotes: 3
Reputation: 7840
I write below mongo query which update multiple documents but , I don't know how to convert this in PHP code but for reference you should use this query
db.collectionName.update({"value":"1234"},{$set:{"name":"example name"}},true,true)
Upvotes: 0