CollectionModify::arrayInsert

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

CollectionModify::arrayInsert配列のフィールドに要素を挿入する

説明

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

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

パラメータ

collection_field

新しい要素が挿入される配列中の要素を識別します。 このパラメータのフォーマットは FIELD_NAME[ INDEX ] です。 FIELD_NAME は、要素を削除するドキュメントフィールドの名前で、 INDEX はフィールド内の要素のインデックスです。

INDEX フィールドは0から始まります。 よって、配列の最初のインデックスは0です。

expression_or_literal

FIELD_NAME[ INDEX ] の後に挿入する新しい要素

戻り値

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

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

<?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')")
->
arrayInsert('traits[1]', 'Happy')
->
execute();

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

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

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

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