我正在尝试使用PHP MongoDB updateMany method将$filter
和$update
数组作为变量传递。
官方文档为updateMany
提供了以下模板:
function updateMany($filter, $update, array $options = []): MongoDB\UpdateResult
将$filter
用作array|object
,将$update
用作array|object
。
当我像这样对updateMany
和$filter
数组进行硬编码时,我能够成功使用$update
:
选项1:在阵列中进行硬编码(成功)
$mongo = new Mongo("mongodb://127.0.0.1:27017/myDB");
$db = new MongoDB\Database($mongo, "myDB");
$collection = $db->selectCollection("articles");
$result = $collection->updateMany(
["_id" => new \MongoDB\BSON\ObjectID("5cae4176af659c1ba03591b5")],
['$set' => ["title" => "update_test"]]
);
但是,当我创建数组变量并尝试将其传递时,它不起作用:
选项2:传入数组变量(错误)
$filter_string = "[\"_id\" => new \\MongoDB\\BSON\\ObjectID(\"5cae4171af659c1ba03591b3\")]";
$update_string = "['\$set' => [\"title\" => \"update_test\"]]";
$filter = explode(',', $filter_string);
$update = explode(',', $update_string);
$mongo = new Mongo("mongodb://127.0.0.1:27017/myDB");
$db = new MongoDB\Database($mongo, "myDB");
$collection = $db->selectCollection("articles");
$result = $collection->updateMany(
[$filter], // Does not work as $filter(without the brackets) either.
[$update] // Does not work as $update (without the brackets) either.
);
选项2错误消息
Fatal error: Uncaught MongoDB\Exception\InvalidArgumentException: First key in $update argument is not an update operator in /var/www/html/mySite/vendor/mongodb/mongodb/src/Operation/UpdateMany.php:82 Stack trace: #0 /var/www/html/mySite/vendor/mongodb/mongodb/src/Collection.php(1005): MongoDB\Operation\UpdateMany->__construct('myDB', 'articles', Array, Array, Array) #1 /var/www/html/mySite/update.php(105): MongoDB\Collection->updateMany(Array, Array) #2 {main} thrown in /var/www/html/mySite/vendor/mongodb/mongodb/src/Operation/UpdateMany.php on line 82
那没有用。因此,我以为如果我将整个数组作为字符串传递,那么PHP可能会自动将字符串中的[]
解释为数组定界符(当然这是行不通的):
选项3:将数组变量作为字符串传递(错误)
$filter_string = "[\"_id\" => new \\MongoDB\\BSON\\ObjectID(\"5cae4171af659c1ba03591b3\")]";
$update_string = "['\$set' => [\"title\" => \"update_test\"]]";
$mongo = new Mongo("mongodb://127.0.0.1:27017/myDB");
$db = new MongoDB\Database($mongo, "myDB");
$collection = $db->selectCollection("articles");
$result = $collection->updateMany(
$filter_string,
$update_string
);
选项3错误消息
Fatal error: Uncaught MongoDB\Exception\InvalidArgumentException: Expected $update to have type "array or object" but found "string" in /var/www/html/mySite/vendor/mongodb/mongodb/src/Exception/InvalidArgumentException.php:32 Stack trace: #0 /var/www/html/mySite/vendor/mongodb/mongodb/src/Operation/UpdateMany.php(78): MongoDB\Exception\InvalidArgumentException::invalidType('$update', '['$set' => ["ti...', 'array or object') #1 /var/www/html/mySite/vendor/mongodb/mongodb/src/Collection.php(1005): MongoDB\Operation\UpdateMany->__construct('myDB', 'articles', '["_id" => new \\...', '['$set' => ["ti...', Array) #2 /var/www/html/mySite/update.php(105): MongoDB\Collection->updateMany('["_id" => new \\...', '['$set' => ["ti...') #3 {main} thrown in /var/www/html/mySite/vendor/mongodb/mongodb/src/Exception/InvalidArgumentException.php on line 32
Tl; dr:我要以哪种格式在PHP $filter
方法中传递$update
和updateMany
变量?
答案 0 :(得分:1)
您可以像这样直接实现
$filterOption = ["_id" => new \MongoDB\BSON\ObjectID("5cae4176af659c1ba03591b5")],
$updateOption = ["title" => "update_test"];
在更新查询中
$result = $collection->updateMany(
$filterOption,
['$set' => $updateOption],
);
这应该有效。