(无版本信息可用,可能仅在 Git 中)
CollectionModify::arrayAppend — 将元素追加到数组字段
$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
)
)
)