定义
参数
$filter: array|object- 指定要删除的文档的筛选条件。
$options: array指定所需选项的数组。
名称类型说明排序规则
数组|对象
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the
localefield is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.如果未指定排序规则但集合具有默认排序规则,则操作将使用为集合指定的排序规则。如果没有为集合或操作指定排序规则,MongoDB 将使用先前版本中用于字符串比较的简单二进制比较。
comment
混合
提示
字符串|数组|对象
要使用的索引。将索引名称指定为字符串或将索引键模式指定为文档。如果指定,查询系统将只考虑使用提示索引的计划。
自 MongoDB 4.4 起此选项可用,如果为旧服务器版本指定,则会在执行时导致异常。
1.7版本新增。
let
数组|对象
参数名称和值的映射。值必须是常量或不引用文档字段的封闭表达式。然后可以在聚合表达式上下文中将参数作为变量进行访问(例如
$$var)。5.0 之前的服务器版本不支持此功能,如果使用,将导致执行时出现异常。
1.13版本新增。
maxTimeMS
整型
The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
投影
数组|对象
The projection specification to determine which fields to include in the returned documents. See Project Fields to Return from Query and Projection Operators in the MongoDB manual.
会话
与操作相关联的客户端会话。
1.3版本新增。
sort
数组|对象
对结果排序的排序规范。
typeMap
阵列
应用游标的类型映射,决定如何将BSON文档转换为PHP值。默认为集合的类型映射。
这将用于返回的结果文档。
writeConcern
Write concern to use for the operation. Defaults to the collection's write concern.
无法将单个操作的写关注(write concern)指定为ACID 事务的一部分。相反,请在启动ACID 事务时设立
writeConcern选项。
Return Values
已删除文档的数组或对象,如果没有文档与查询匹配,则为null 。 返回类型将取决于typeMap选项。
错误/异常
MongoDB\Exception\UnexpectedValueException ,如果来自服务器的命令响应格式错误。
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
行为
在评估查询条件时, MongoDB会根据自己的BSON 类型比较规则来比较类型和值,这与 PHP 的比较和类型规则不同。匹配特殊BSON类型时,查询条件应使用扩展中相应的BSON类(例如使用MongoDB\BSON\ObjectId来匹配ObjectId)。
示例
以下示例从数据库test的collectionrestaurants中查找并删除restaurant_id为"40375376"的文档:
$collection = (new MongoDB\Client)->test->restaurants; $deletedRestaurant = $collection->findOneAndDelete( [ 'restaurant_id' => '40375376' ], [ 'projection' => [ 'name' => 1, 'borough' => 1, 'restaurant_id' => 1, ], ] ); var_dump($deletedRestaurant);
而输出将类似如下所示:
object(MongoDB\Model\BSONDocument)#17 (1) { ["storage":"ArrayObject":private]=> array(4) { ["_id"]=> object(MongoDB\BSON\ObjectId)#11 (1) { ["oid"]=> string(24) "594d5ef280a846852a4b3f70" } ["borough"]=> string(9) "Manhattan" ["name"]=> string(15) "Agra Restaurant" ["restaurant_id"]=> string(8) "40375376" } }
另请参阅
MongoDB 手册中的findAndModify命令参考