©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PECL mongo >=0.9.0)
MongoCollection::remove — 从集合中删除记录
$criteria
= array()
[, array $options
= array()
]] )
criteria
待删除记录的描述。
options
删除的选项。
"w"
See WriteConcerns. The default value for MongoClient is 1.
"justOne"
最多只删除一个匹配的记录。
"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" 选项,将会返回包含删除状态的 array。
否则返回 TRUE
。
状态数组字段的解释位于 MongoCollection::insert() 的文档。
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 值来代表 "justOne"。
现在,必须使用 array('justOne' => true) 作为替代。
|
1.2.11 |
当 options 是 scalar 时产生一个 E_DEPRECATED 警告。
|
1.2.0 | 添加 "timeout" 选项。 |
1.0.11 | 在设置了 "safe" 之后,将在出现 "not master" 错误时断开连接。 |
1.0.9 | 添加了 "safe" 选项对 integer 的支持,之前只接受 boolean 值。 添加了 "fsync" 选项。 当使用了 "safe" 选项时将会返回包含错误信息的数组。 否则和之前一样返回一个 boolean。 |
1.0.5 | 修改第二个参数为选项的 array。在 1.0.5 之前,第二个选项是 boolean 值, 代表了 "safe" 选项。 |
Example #1 MongoCollection::remove() 的 justOne 例子
<?php
$radioactive = $db -> radioactive ;
// 统计那有多少个钚
$remaining = $radioactive -> count (array( 'type' => 94 ));
$halflife = $remaining / 2 ;
// 删除一半
while ( $halflife > 0 ) {
$radioactive -> remove (array( 'type' => 94 ), array( "justOne" => true ));
$halflife --;
}
?>
[#1] konstantin718 at gmail dot com [2013-01-21 03:17:08]
If you try to remove a document that doesn't exist, remove() won't throw an exception.
Even if 'w' is set to 1, remove() will return an array similar to this:
Array ( [n] => 0 [connectionId] => 10726 [err] => [ok] => 1 )
So, in order to see if a remove() was really successful, you need to look at [n] key. If it's 0, that means the remove() did not remove any documents.
If remove() is successful, [n] will be > 0.
[#2] David Winter [2010-01-02 05:55:09]
To remove a document based on its ID, you need to ensure that you pass the ID as a MongoID object rather than just a string:
<?php
$id = '4b3f272c8ead0eb19d000000';
// will not work:
$collection->remove(array('_id' => $id), true);
// will work:
$collection->remove(array('_id' => new MongoId($id)), true);
?>