(無版本資訊,可能僅存在於 Git 中)
CollectionModify::arrayAppend — 將元素附加到陣列欄位
$collection_field
, 字串 $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 ) ) )