(PECL mongo >=0.9.0)
MongoCollection::insert — Inserts an array into the collection
$a
[, array $options
= array()
] )All strings sent to the database must be UTF-8. If a string is not UTF-8, a MongoException will be thrown. To insert (or query for) a non-UTF-8 string, use MongoBinData.
a
An array.
options
Options for the insert.
"w"
See WriteConcerns. The default value for MongoClient is 1.
"fsync"
Boolean, defaults to FALSE
. Forces the insert to be synced to disk before returning success. If TRUE
, an acknowledged insert is implied and will override setting w to 0.
"timeout"
Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, a MongoCursorTimeoutException will be thrown.
"safe"
Deprecated. Please use the WriteConcern w option.
Returns an array containing the status of the insertion if the
"w" option is set. Otherwise, returns TRUE
if the
inserted array is not empty (a MongoException will be
thrown if the inserted array is empty).
If an array is returned, the following keys may be present:
ok
This should almost be 1 (unless last_error itself failed).
err
If this field is non-null, an error occurred on the previous operation. If this field is set, it will be a string describing the error that occurred.
code
If a database error occurred, the relevant error code will be passed back to the client.
errmsg
This field is set if something goes wrong with a database command. It is coupled with ok being 0. For example, if w is set and times out, errmsg will be set to "timed out waiting for slaves" and ok will be 0. If this field is set, it will be a string describing the error that occurred.
n
If the last operation was an insert, an update or a remove, the number of objects affected will be returned.
wtimeout
If the previous option timed out waiting for replication.
waited
How long the operation waited before timing out.
wtime
If w was set and the operation succeeded, how long it took to replicate to w servers.
upserted
If an upsert occured, this field will contain the new record's _id field. For upserts, either this field or updatedExisting will be present (unless an error occurred).
updatedExisting
If an upsert updated an existing element, this field will be true. For upserts, either this field or upserted will be present (unless an error occurred).
Throws MongoException if the inserted array is empty.
Throws MongoCursorException if the "w" option is set and the write fails.
Throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
Wersja | Opis |
---|---|
1.3.0 |
The options parameter no longer accepts a boolean
to signify a acknowledged write. Instead, this now has to be done with
array('w' => 1) (The default behaviour of
MongoClient).
|
1.2.0 | Added "timeout" option. |
1.0.11 | Disconnects on "not master" errors if "safe" is set. |
1.0.9 |
Added ability to pass integers to the "safe" option, which previously only accepted booleans. Added "fsync" option. The return type was changed to be an array containing error information if the "safe" option is used. Otherwise, a boolean is returned as before. |
1.0.5 | Changed second parameter to be an array of options. Pre-1.0.5, the second parameter was a boolean indicating the "safe" option. |
1.0.1 | Throw a MongoCursorException if the "safe" option is set and the insert fails. |
Przykład #1 MongoCollection::insert() _id example
Inserting an object will add an _id field to it, unless it is passed by reference.
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$a = array('x' => 12);
$collection->insert($a);
var_dump($a);
$b = array('x' => 12);
$ref = &$b;
$collection->insert($ref);
var_dump($ref);
?>
Powyższy przykład wyświetli coś podobnego do:
array(2) { ["x"]=> int(12) ["_id"]=> object(MongoId)#4 (0) { } } array(12) { ["x"]=> int(1) }
Przykład #2 MongoCollection::insert() acknowledged write example
This example shows inserting two elements with the same _id, which causes
a MongoCursorException to be thrown, as
w
was set.
<?php
$person = array("name" => "Joe", "age" => 20);
$collection->insert($person);
// now $person has an _id field, so if we save it
// again, we will get an exception
try {
$collection->insert($person, array("w" => 1));
} catch(MongoCursorException $e) {
echo "Can't save the same person twice!\n";
}
?>