©
本文档使用
php.cn手册 发布
(PECL mongo >=0.9.0)
MongoCollection::insert — 插入文档到集合中
$a
[, array $options
= array()
] )发送到数据库的所有字符串必须是 UTF-8 的。如果有字符串不是 UTF-8,将会抛出 MongoException 异常。 要插入(或者查询)一个非 UTF-8 的字符串,请使用 MongoBinData。
a
一个数组或对象。如果使用的是一个对象,它不能有 protected 或 private 的属性。
Note:
如果参数不具有 _id 键或属性, 将会创建一个新的 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
代表数组不是空的(空数组将会抛出 MongoException )。
如果返回了一个 array,将会有以下键:
ok
它应该几乎总是 1(除非 last_error 本身出现错误)。
err
如果这个字段不是 null,说明刚才的操作出现了错误。 如果有这个字段,它将包含一个字符串,用于描述出现的错误信息。
code
如果发生了一个数据库错误,相应的错误码会传到客户端。
errmsg
如果数据库命令出现了错误,将会设置这个字段。同时 ok 也会是 0. 例如,设置了 w 并且超时了,errmsg 将会是 "timed out waiting for slaves" 并且 ok 是 0。 如果设置了这个字段,它会是发生的错误的字符串描述。
n
如果最后的操作是插入、更新或删除,将会返回受影响的对象数量。对于插入操作,这个值总是 0。
wtimeout
等待复制直到超时的时间。
waited
在超时前,要等待操作多久。
wtime
如果设置了 w 并且操作成功了,等到复制到 w 台服务器的时间。
upserted
如果发生了一次 upsert,这个字段将会包含新记录的 _id。 对于 upsert,不管是该字段还是 updatedExisting 都会被保留(除非发生了一个错误)。
updatedExisting
如果一个 upsert 更新了一个存在的元素,这个字段将会是 true。 对于 upsert,无论是这个字段 还是 upserted 都会被保留(除非发生了错误)。
如果插入的文档是空的,或者包含零长度的键,将会抛出 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 来标识一个确认的写入。
现在,你可以通过
array('w' => 1) 设置(
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" 选项并且插入失败了,将会抛出 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 字段,所以我们再次
// 保存它的时候,将会得到一个异常
try {
$collection -> insert ( $person , array( "w" => 1 ));
} catch( MongoCursorException $e ) {
echo "Can't save the same person twice!\n" ;
}
?>
[#1] mike at eastghost dot com [2014-05-25 03:53:19]
Attempting to insert() an array containing a NULL value followed by a blank space, in a non-utf8 encoding, results in the entire array (and all of its data) being ignored and the $opts array parameter being substituted instead as the data.
[#2] root at php dot net [2013-04-04 11:06:22]
_id and MongoId can be a source of problems that can make what would seem a trivial operation potentially complicated.
MongoId is not as predictable or safe as mysql's auto increment (an example that most PHP developers will be familiar with). _id is generated by the client rather than the server and so does not guarantee that it will be collision free.
By comparison, server side auto_increment mechanisms that PHP programmers might typically be used to wont collide until every single id had been used and with 64bits you can ensure this will almost never happen. You will also know when your table is getting full, and you can predict the rate. Most importantly, no matter the mechanism, being server side guarantees two clients wont collide. Mongo's behaviour is different to this.
Generally speaking inserting without specifying _id will tend to work, but there are some cases where is can fail or is particularly prone to failure.
The total size I believe is 96 bits. This might seem like a lot but the value is not created randomly. It is generated like this:
$unixtime . $machine_id . $pid . $counter
The counter starts from zero and is attached to each instance of MongoClient thus two MongoClient connections to the same server will almost certainly not work (produce a collision):
$m=new MongoWrapper();
$m->insert([0]);
$m=new MongoWrapper();
$m->insert([1]);
If MongoWrapper is not using a singleton for the connection or something to the same effect, the second call will most likely have the same unixtime. It will certainly have the same machine_id, pid and counter. The insert will fail.
If you are not using a singleton, this will work:
$m=new MongoWrapper();
$m->insert([0]);
$m->insert([1]);
You may also have difficulties in a multiple machine environment.
machine_id is a hash of gethostname. This is not guaranteed to be unique across machines. Some people do not set hostnames at all. If you do not ensure that your machines all have unique hostnames then if in the same second, two machines run a script that inserts, the second will have a 1 in 2^15 chance of colliding (assuming the most common PID max). Depending on how the system handles pids, the probability may actually be a little less. In short, make sure any host accessing your mongodb has a hostname that is unique among any other host accessing your mongodb.
I've seen some specs specify that counter should start from a random value but I highly recommend against this as it merely hides/obscures the problem.
[#3] gmail.com@mspreij [2013-01-22 16:53:14]
"Note: If the parameter does not have an _id key or property, a new MongoId instance will be created and assigned to it."
Note on note: this is true even if the insert *fails* (because of, say, duplicate key error). So even if no new document was inserted, the supplied array will still have a new MongoID key ->_id after the ->insert().
(which can make an attempted update after that fail, because you cannot update the _id value of a document..)
[#4] cuisdy at gmail dot com [2012-11-30 17:21:16]
Another rarity to keep in mind. Passing references has the same effect as passing a referenced object. Even if there's no different for PHP between those two, it's probably not evident. So, this code would not have the _id field added to $a:
<?php
$b = &$a;
$m = new MongoClient;
$collection = $m->test->phpmanual;
$a = array('x' => 12);
$collection->insert($a);
var_dump($a);
// array(1) { ["x"]=> int(12) }
?>
I've made the assignment above to show how this situation could happen, if you reassigned a var, for example. But it could be some normal referencing after giving $a its final value (but before calling insert), and the consequence would be the same: if a var is referenced, it won't get the _id appended.
[#5] John S. [2012-08-29 14:10:49]
Note, that the _id field will only be added to an inserted array if it does not already exist in the supplied array:
<?php
$data = array('x' => 12);
var_dump($data);
$collection->insert($data);
var_dump($data);
?>
Will output something like:
array(1) {
["x"]=>
int(12)
}
array(2) {
["x"]=>
int(12)
["_id"]=>
object(MongoId)#196 (1) {
["$id"]=>
string(24) "503e21fc0605290912000000"
}
}
however,
$data = array('x' => 12, '_id' => NULL);
var_dump($data);
$collection->insert($data);
var_dump($data);
will not have the same result:
array(2) {
["x"]=>
int(12)
["_id"]=>
NULL
}
array(2) {
["x"]=>
int(12)
["_id"]=>
NULL
}
[#6] Christer Edvartsen [2011-04-01 08:54:27]
Also worth noting is that the MongoCollection::insert() method accepts objects as the first argument as well as arrays.
<?php
$data = new stdClass;
$data->foo = 'foo';
$data->bar = 'bar';
$collection->insert($data);
var_dump($data->_id); // An instance of MongoId
?>
You can use other classes as well, but MongoCollection::insert() will fail if the object contains any protected or private properties. Public properties listed in the class will also be inserted:
<?php
class SomeClass {
public $foo = 'bar';
public $bar = 'foo';
}
$data = new SomeClass;
$data->foobar = 42;
$collection->insert($data);
var_dump($data->_id); // An instance of MongoId
?>
will result in a document with four elements:
_id => some mongoid
foo => 'bar'
bar => 'foo'
foobar => 42