PHP MongoDB updateManyメソッドを使用して$filter
、$update
配列を変数として渡そうとしています。
公式ドキュメントには、次のテンプレートが用意されていますupdateMany
。
function updateMany($filter, $update, array $options = []): MongoDB\UpdateResult
$filter
などarray|object
と$update
としてarray|object
。
と配列を次のようupdateMany
にハードコーディングする$filter
と、正常に使用でき$update
ます。
オプション1:アレイのハードコード(SUCCESS)
$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
ますか?
あなたはこのように直接達成することができます、
$filterOption = ["_id" => new \MongoDB\BSON\ObjectID("5cae4176af659c1ba03591b5")],
$updateOption = ["title" => "update_test"];
そしてあなたの更新クエリで
$result = $collection->updateMany(
$filterOption,
['$set' => $updateOption],
);
これはうまくいくはずです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加