(PECL mongo >=0.9.0)
MongoCollection::insert — 锟斤拷锟斤拷锟侥碉拷锟斤拷锟斤拷锟斤拷锟斤拷
锟斤拷锟酵碉拷锟斤拷锟捷匡拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷锟� UTF-8 锟侥★拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟� UTF-8锟斤拷锟斤拷锟斤拷锟阶筹拷 MongoException 锟届常锟斤拷 要锟斤拷锟诫(锟斤拷锟竭诧拷询锟斤拷一锟斤拷锟斤拷 UTF-8 锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷使锟斤拷 MongoBinData锟斤拷
a
一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷玫锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� protected 锟斤拷 private 锟斤拷锟斤拷锟皆★拷
Note:
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� _id 锟斤拷锟斤拷锟斤拷锟皆o拷 锟斤拷锟结创锟斤拷一锟斤拷锟铰碉拷 MongoId 实锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷味锟脚诧拷锟斤拷锟角达拷锟斤拷锟矫的★拷
options
锟斤拷锟斤拷锟窖★拷睢�
"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.
"j"
Boolean, defaults to FALSE
. Forces the write operation to block until it is synced to the journal on disk. If TRUE
, an acknowledged write is implied and this option will override setting "w" to 0.
Note: If this option is used and journaling is disabled, MongoDB 2.6+ will raise an error and the write will fail; older server versions will simply ignore the option.
"w"
See WriteConcerns. The default value for MongoClient is 1.
"wtimeout"
Deprecated alias for "wTimeoutMS".
"safe"
Deprecated. Please use the WriteConcern w option.
"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.
锟斤拷锟斤拷锟斤拷锟斤拷锟� "w" 选锟筋,锟斤拷锟结返锟截帮拷锟斤拷锟斤拷锟斤拷状态锟斤拷锟斤拷锟介。
锟斤拷锟津,斤拷锟结返锟斤拷一锟斤拷 TRUE
锟斤拷锟斤拷锟斤拷锟介不锟角空的o拷锟斤拷锟斤拷锟介将锟斤拷锟阶筹拷 MongoException 锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟� array锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰硷拷锟斤拷
ok
锟斤拷应锟矫硷拷锟斤拷锟斤拷锟斤拷 1锟斤拷锟斤拷锟斤拷 last_error 锟斤拷锟斤拷锟斤拷执锟斤拷螅锟�
err
锟斤拷锟斤拷锟斤拷锟街段诧拷锟斤拷 null锟斤拷说锟斤拷锟秸才的诧拷锟斤拷锟斤拷锟斤拷锟剿达拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟街段o拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街的达拷锟斤拷锟斤拷息锟斤拷
code
锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷菘锟斤拷锟斤拷锟斤拷应锟侥达拷锟斤拷锟斤拷岽拷锟斤拷突锟斤拷恕锟�
errmsg
锟斤拷锟斤拷锟斤拷菘锟斤拷锟斤拷锟斤拷锟斤拷锟剿达拷锟襟,斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷侄巍锟酵� ok 也锟斤拷锟斤拷 0. 锟斤拷锟界,锟斤拷锟斤拷锟斤拷 w 锟斤拷锟揭筹拷时锟剿o拷errmsg 锟斤拷锟斤拷锟斤拷 "timed out waiting for slaves" 锟斤拷锟斤拷 ok 锟斤拷 0锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街段o拷锟斤拷锟斤拷锟角凤拷锟斤拷锟侥达拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷锟�
n
锟斤拷锟斤拷锟斤拷牟锟斤拷锟斤拷遣锟斤拷搿拷锟斤拷禄锟缴撅拷锟斤拷锟斤拷锟斤拷岱碉拷锟斤拷锟接帮拷锟侥讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷 0锟斤拷
wtimeout
锟饺达拷锟斤拷锟斤拷直锟斤拷锟斤拷时锟斤拷时锟戒。
waited
锟节筹拷时前锟斤拷要锟饺达拷锟斤拷锟斤拷锟斤拷谩锟�
wtime
锟斤拷锟斤拷锟斤拷锟斤拷锟� w 锟斤拷锟揭诧拷锟斤拷锟缴癸拷锟剿o拷锟饺碉拷锟斤拷锟狡碉拷 w 台锟斤拷锟斤拷锟斤拷锟斤拷时锟戒。
upserted
锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟� upsert锟斤拷锟斤拷锟斤拷侄谓锟斤拷锟斤拷锟斤拷锟铰硷拷录锟斤拷 _id锟斤拷 锟斤拷锟斤拷 upsert锟斤拷锟斤拷锟斤拷锟角革拷锟街段伙拷锟斤拷 updatedExisting 锟斤拷锟结被锟斤拷锟斤拷锟斤拷锟斤拷锟角凤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟襟)★拷
updatedExisting
锟斤拷锟揭伙拷锟� upsert 锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟节碉拷元锟截o拷锟斤拷锟斤拷侄谓锟斤拷锟斤拷锟� true锟斤拷 锟斤拷锟斤拷 upsert锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷侄锟� 锟斤拷锟斤拷 upserted 锟斤拷锟结被锟斤拷锟斤拷锟斤拷锟斤拷锟角凤拷锟斤拷锟剿达拷锟襟)★拷
锟斤拷锟斤拷锟斤拷锟斤拷锟侥碉拷锟角空的o拷锟斤拷锟竭帮拷锟斤拷锟姐长锟饺的硷拷锟斤拷锟斤拷锟斤拷锟阶筹拷 MongoException锟斤拷 锟斤拷锟皆诧拷锟斤拷锟斤拷锟� protected 锟斤拷 private 锟斤拷锟皆的讹拷锟襟将会导锟斤拷锟姐长锟饺硷拷锟斤拷zero-length key锟斤拷锟侥达拷锟斤拷
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.
锟芥本 | 说锟斤拷 |
---|---|
1.3.0 |
options 锟斤拷锟斤拷锟斤拷锟劫斤拷锟斤拷 boolean 锟斤拷锟斤拷识一锟斤拷确锟较碉拷写锟诫。
锟斤拷锟节o拷锟斤拷锟斤拷锟酵拷锟�
array('w' => 1) 锟斤拷锟矫o拷
MongoClient 默锟较碉拷锟斤拷为锟斤拷
|
1.2.0 | 锟斤拷锟斤拷锟斤拷 "timeout" 选锟筋。 |
1.0.11 | 锟斤拷锟斤拷锟斤拷锟斤拷锟� "safe"锟斤拷锟斤拷锟斤拷 "not master" 锟斤拷锟斤拷时锟较匡拷锟斤拷锟接★拷 |
1.0.9 |
"safe" 选锟斤拷锟斤拷锟� integer 值锟斤拷之前只锟斤拷锟斤拷 boolean 值锟斤拷 锟斤拷锟斤拷 "fsync" 选锟筋。 锟斤拷锟斤拷锟斤拷锟斤拷锟� "safe" 选锟筋,锟斤拷锟斤拷锟斤拷锟酵改成帮拷锟斤拷锟斤拷锟斤拷锟斤拷息锟斤拷 array锟斤拷 锟斤拷锟津,猴拷之前一锟斤拷锟斤拷锟斤拷 boolean锟斤拷 |
1.0.5 | 锟睫改第讹拷锟斤拷锟斤拷锟斤拷为选锟斤拷锟斤拷锟介。锟斤拷 1.0.5 之前锟斤拷锟节讹拷锟斤拷锟斤拷锟斤拷锟斤拷 boolean锟斤拷指示 "safe" 选锟筋。 |
1.0.1 | 锟斤拷锟斤拷锟斤拷锟斤拷锟� "safe" 选锟筋并锟揭诧拷锟斤拷失锟斤拷锟剿o拷锟斤拷锟斤拷锟阶筹拷 MongoCursorException锟斤拷 |
Example #1 MongoCollection::insert() _id 锟斤拷锟斤拷
锟斤拷锟矫伙拷锟� _id锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷牡锟斤拷小锟� 锟斤拷锟节诧拷锟斤拷锟斤拷锟斤拷姆锟绞斤拷锟斤拷诘锟斤拷么锟斤拷锟绞憋拷锟斤拷锟斤拷薹锟斤拷锟斤拷锟斤拷锟叫� _id锟斤拷
<?php
$m = new MongoClient();
$collection = $m->selectCollection('test', 'phpmanual');
// 锟斤拷锟绞癸拷玫锟斤拷锟� array 锟斤拷锟斤拷锟斤法锟斤拷锟斤拷锟睫凤拷锟缴癸拷锟斤拷锟斤拷 _id
$collection->insert(array('x' => 1));
// 通锟斤拷锟斤拷 array 锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 _id
$a = array('x' => 2);
$collection->insert($a);
var_dump($a);
// 通锟斤拷锟斤拷锟斤拷锟矫碉拷 array 锟睫凤拷锟斤拷锟斤拷 _id
$b = array('x' => 3);
$ref = &$b;
$collection->insert($ref);
var_dump($ref);
// 没锟斤拷锟节帮拷锟斤拷锟侥猴拷锟斤拷锟叫达拷锟斤拷 copy-on-write 时 _id 锟斤拷效
function insert_no_cow($collection, $document)
{
$collection->insert($document);
}
$c = array('x' => 4);
insert_no_cow($collection, $c);
var_dump($c);
// 锟节帮拷锟斤拷锟侥猴拷锟斤拷锟叫达拷锟斤拷 copy-on-write 时 _id 锟斤拷效
function insert_cow($collection, $document)
{
$document['y'] = 1;
$collection->insert($document);
}
$d = array('x' => 5);
insert_cow($collection, $d);
var_dump($d);
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
array(2) { ["x"]=> int(2) ["_id"]=> object(MongoId)#4 (0) { } } array(1) { ["x"]=> int(3) } array(2) { ["x"]=> int(4) ["_id"]=> object(MongoId)#5 (0) { } } array(1) { ["x"]=> int(5) }
Example #2 MongoCollection::insert() 确锟斤拷写锟斤拷锟斤拷锟斤拷锟�
锟斤拷锟斤拷锟斤拷锟斤拷锟绞撅拷锟斤拷锟斤拷锟斤拷锟� w
锟襟,诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷同 _id 锟斤拷元锟斤拷时锟斤拷锟斤拷锟斤拷锟阶筹拷 MongoCursorException 锟斤拷锟斤拷锟接★拷
<?php
$person = array("name" => "Joe", "age" => 20);
$collection->insert($person);
// 锟斤拷锟斤拷 $person 锟斤拷锟斤拷一锟斤拷 _id 锟街段o拷锟斤拷锟斤拷锟斤拷锟斤拷锟劫达拷
// 锟斤拷锟斤拷锟斤拷锟斤拷时锟津,斤拷锟斤拷玫锟揭伙拷锟斤拷斐�
try {
$collection->insert($person, array("w" => 1));
} catch(MongoCursorException $e) {
echo "Can't save the same person twice!\n";
}
?>