(沒有版本資訊,可能只存在於 Git 中)
CollectionModify::arrayInsert — 將元素插入陣列欄位
$collection_field
, 字串 $expression_or_literal
): mysql_xdevapi\CollectionModify將元素新增至文件的欄位,因為欄位的多個元素以陣列表示。 與 mysql_xdevapi\CollectionModify::arrayAppend() 不同,此方法允許透過定義新元素位於哪個項目之後來指定插入新元素的位置,而 mysql_xdevapi\CollectionModify::arrayAppend() 總是將新元素附加到陣列的末尾。
集合欄位 (collection_field)
識別陣列中要在其後插入新元素的項目。此參數的格式為 欄位名稱[ 索引 ]
,其中 欄位名稱 是要添加元素的文檔欄位的名稱,索引 是欄位內元素的索引。
索引欄位是從零開始的,因此陣列的第一個項目的索引為 0。
表達式或字面值 (expression_or_literal)
要在 欄位名稱[ 索引 ] 之後插入的新元素
一個 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 ) ) )