CollectionModify::arrayInsert

(사용 가능한 버전 정보가 없으며 Git에만 있을 수 있음)

CollectionModify::arrayInsert — 배열 필드에 요소 삽입


설명

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

필드의 여러 요소가 배열로 표시되므로 문서의 필드에 요소를 추가합니다. arrayAppend()와 달리 arrayInsert()를 사용하면 뒤에 오는 항목을 정의하여 새 요소가 삽입되는 위치를 지정할 수 있지만 arrayAppend()는 항상 배열 끝에 새 요소를 추가합니다.


매개변수

collection_field
배열에서 새 요소가 뒤에 삽입되는 항목을 식별합니다. 이 매개변수의 형식은 FIELD_NAME[ INDEX ]입니다. 여기서 FIELD_NAME은 요소를 제거할 문서 필드의 이름이고 INDEX는 필드 내 요소의 INDEX입니다.

INDEX 필드는 0부터 시작하므로 배열에서 가장 왼쪽에 있는 항목의 인덱스는 0입니다.

expression_or_literal
FIELD_NAME[ INDEX ] 뒤에 삽입할 새 요소

반환 값

명령을 실행하거나 추가 작업을 추가하는 데 사용할 수 있는 CollectionModify 개체


Examples

예제 #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
                )
        )
)