CollectionModify::arrayInsert
(No version information available, might only be in Git)
CollectionModify::arrayInsert — Insert element into an array field
Description
$collection_field
, string $expression_or_literal
) : mysql_xdevapi\CollectionModifyAdd an element to a document's field, as multiple elements of a field are represented as an array. Unlike arrayAppend(), arrayInsert() allows you to specify where the new element is inserted by defining which item it is after, whereas arrayAppend() always appends the new element at the end of the array.
Parameters
-
collection_field
-
Identify the item in the array that the new element is inserted after. The format of this parameter is FIELD_NAME[ INDEX ] where FIELD_NAME is the name of the document field to remove the element from, and INDEX is the INDEX of the element within the field.
The INDEX field is zero based, so the leftmost item from the array has an index of 0.
-
expression_or_literal
-
The new element to insert after FIELD_NAME[ INDEX ]
Return Values
A CollectionModify object that can be used to execute the command, or to add additional operations
Examples
Example #1 mysql_xdevapi\CollectionModify::arrayInsert() example
<?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());
?>
The above example will output something similar to:
Array ( [0] => Array ( [_id] => 00005b6b5361000000000000010d [name] => Bernie [traits] => Array ( [0] => Friend [1] => Happy [2] => Brother [3] => Human ) ) )
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mysql-xdevapi-collectionmodify.arrayinsert.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.