(PECL mongo >=0.9.0)



public MongoCollection::remove ( array $criteria = array() , array $options = array() ) : bool|array






  • "w"

    See Write Concerns. The default value for MongoClient is 1.

  • "justOne"


  • "fsync"

    Boolean, defaults to false. If journaling is enabled, it works exactly like "j". If journaling is not enabled, the write operation blocks until it is synced to database files on disk. If true, an acknowledged insert is implied and this option will override setting "w" to 0.

    注意: If journaling is enabled, users are strongly encouraged to use the "j" option instead of "fsync". Do not use "fsync" and "j" simultaneously, as that will result in an error.

  • "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.

    注意: 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 Write Concerns. The default value for MongoClient is 1.

  • "wtimeout"

    Deprecated alias for "wTimeoutMS".

  • "safe"

    Deprecated. Please use the write concern "w" option.

  • "timeout"

    Deprecated alias for "socketTimeoutMS".


如果设置了 "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" 错误时断开连接。

添加了 "safe" 选项对 integer 的支持,之前只接受 boolean 值。

添加了 "fsync" 选项。

当使用了 "safe" 选项时将会返回包含错误信息的数组。 否则和之前一样返回一个 boolean。

1.0.5 修改第二个参数为选项的 array。在 1.0.5 之前,第二个选项是 boolean 值, 代表了 "safe" 选项。


示例 #1 MongoCollection::remove() 的 justOne 例子



// 统计那有多少个钚
$remaining $radioactive->count(array('type' => 94));

$halflife $remaining/2;

// 删除一半
while ($halflife 0) {
$radioactive->remove(array('type' => 94), array("justOne" => true));



add a note add a note

User Contributed Notes 2 notes

David Winter
11 years ago
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:

= '4b3f272c8ead0eb19d000000';

// will not work:
$collection->remove(array('_id' => $id), true);

// will work:
$collection->remove(array('_id' => new MongoId($id)), true);
konstantin718 at gmail dot com
8 years ago
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.
To Top