(No version information available, might only be in Git)
Collection::add — Add collection document
Triggers the insertion of the given document(s) into the collection, and multiple variants of this method are supported. Options include:
Add a single document as a JSON string.
Add a single document as an array as: [ 'field' => 'value', 'field2' => 'value2' ... ]
A mix of of both, and multiple documents can be added in the same operation.
document
One or multiple documents, and this can be either JSON or an array of fields with their associated values. This cannot be an empty array.
The MySQL server automatically generates unique _id values for each document, although this can be manually added as well. This value must be unique as otherwise the add operation will fail.
A Result object that can be used to query the number of affected items, the number warnings generated by the operation, or to fetch a list of generated IDs for the inserted documents.
Example #1 mysql_xdevapi\Collection::add() example
<?php
$schema = $session->getSchema("test");
$coll = $schema->getCollection("test_collection");
// Add two documents, and _id is generated by the server
$coll->add('{"name": "Marco", "age": 19, "job": "Programmatore"}')->execute();
$coll->add('{"name": "Lonardo", "age": 59, "job": "Paninaro"}')->execute();
// Two two more documents but manually provide _id
// Note: manually adding _id is not recommended, and they must be unique
$coll->add(
["_id" => "1", "name" => "Sakila", "age" => 18, "job" => "Student"],
["_id" => "2", "name" => "Mike", "age" => 39, "job" => "Manager"]
)->execute();
// Add two more documents using a single JSON object
$res = $coll->add(
'{"name": "Marco",
"jobs": [{"title":"Mangiatore","Salary":1000},{"title":"Ciarlatano","Salary":12000}],
"hobby": ["Spavare","Soffiare Minestrine"], "code":0}',
'{"name": "Lucrezia",
"jobs": [{"title":"Urlatrice","Salary":2000},{"title":"Parlatrice","Salary":3400}],
"hobby": ["Cucinare","Guidare auto sportive","Cavalcare"], "code":1}')->execute();
// Fetch a list of generated ID's and print
$ids = $res->getGeneratedIds();
print_r($ids);
?>
以上例程的输出类似于:
Array ( [0] => 00005ad4d63c000000000000000a [1] => 00005ad4d63c000000000000000b )