MongoCollection::remove

(PECL mongo >=0.9.0)

MongoCollection::remove从集合中删除记录

说明

public MongoCollection::remove ([ array $criteria = array() [, array $options = array() ]] ) : bool|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 optionsscalar 时产生一个 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--;
}

?>

参见

关注编程学问公众号