使用$ set和php进行多重更新Mongo
问题描述:
我试图在mongolab平台上使用multi = true参数的update()函数更新集合中的多个项目。 问题是只有收集中的第一项更新为。
集合:
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "first"
},
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "second"
}
Script代码:
$db->collection->update(
array('value' => '1234'),
array('$set' => array(
'name' => 'example name',
)),
array('multi' => true)
);
结果:
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "example name"
},
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "second"
}
更新()函数只接受三个阵列作为参数。
答
我下面写蒙戈查询其更新多个文件,但我不知道如何在这个PHP代码转换,但供参考,你应该使用此查询
db.collectionName.update({"value":"1234"},{$set:{"name":"example name"}},true,true)
答
嗨约杰什,
我觉得您的查询应该是:
db.collectionName.update({"value":"1234"},{$set:{"name":"example name"}},{'upsert':true, 'multiple': true});
相应的PHP代码将是:
<?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);
?>
我希望这就是你要找的。
该说明书是您的朋友。检查正确的语法。 – 2014-11-02 15:54:36
用“multiple”=>“true”而不是“multi”解决。 来源:http://*.com/questions/7934119/php-mongodb-update-multiple-documents-using-in-or或 – 2014-11-02 15:59:17
@Hadokee,请添加,作为你的问题的答案。 – helmy 2014-11-03 02:25:01