CollectionModify::arrayAppend

(No version information available, might only be in Git)

CollectionModify::arrayAppend配列のフィールドに要素を追加する

説明

publicmysql_xdevapi\CollectionModify::arrayAppend(string$collection_field, string$expression_or_literal): mysql_xdevapi\CollectionModify

ドキュメントのフィールドに要素を追加します。 フィールドの複数の要素は配列として表現されます。 arrayInsert() と異なり、arrayAppend() は常に新しい要素を配列の最後に追加します。 一方で、arrayInsert() は挿入する位置を指定できます。

パラメータ

collection_field

新しい要素を挿入するフィールドの識別子

expression_or_literal

ドキュメントフィールドの配列の最後に挿入する新しい要素

戻り値

コマンドを実行したり、追加の操作を追加できる CollectionModify オブジェクトを返します。

例1 mysql_xdevapi\CollectionModify::arrayAppend() の例

<?php
$session
= mysql_xdevapi\getSession("mysqlx://user:password@localhost");
$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();

$schema = $session->getSchema("addressbook");
$collection = $schema->createCollection("people");

$result = $collection
->add(
'{"name": "Bernie",
"traits": ["Friend", "Brother", "Human"]}'
)
->
execute();

$collection
->modify("name in ('Bernie', 'Jane')")
->
arrayAppend('traits', 'Happy')
->
execute();

$result = $collection
->find()
->
execute();

print_r($result->fetchAll());
?>

上の例の出力は、 たとえば以下のようになります。

Array ( [0] => Array ( [_id] => 00005b6b5361000000000000010c [name] => Bernie [traits] => Array ( [0] => Friend [1] => Brother [2] => Human [3] => Happy ) ) )
To Top