MongoDB\Collection::findOneAndDelete()
定义
参数
$filter
: array|object- 指定要删除的文档的过滤器条件。
$options
: array指定所需选项的数组。
名称类型描述collationarray|object注释混合提示字符串|数组|对象要使用的索引。指定索引名称作为字符串或索引键模式作为文档。如果指定,则查询系统将只考虑使用提示索引的计划。
此选项自MongoDB 4.4起可用,如果为较旧的服务器版本指定,则会在执行时引发异常。
新版本1.7.
letarray|object参数名称和值的映射。值必须是常数或封闭表达式,不能引用文档字段。参数然后可以在聚合表达式上下文中作为变量访问(例如
$$var
)。此功能不支持5.0之前的版本,如果使用将导致执行时引发异常。
新版本1.13.
maxTimeMS整数处理游标上操作的总时间限制(以毫秒为单位)。MongoDB在最早的中断点之后终止操作。
projectionarray|objectsession与操作关联的客户端会话。
新版本1.3.
sortarray|object结果排序的排序规范。typeMap数组将类型映射应用于游标,该游标决定了BSON文档如何转换为PHP值。默认为集合的类型映射。
这将用于返回的文档结果。
写关注
返回值
被删除的文档的数组或对象,或如果没有文档匹配查询,则为null
。返回类型将取决于typeMap
选项。
错误/异常
MongoDB\Exception\UnexpectedValueException
如果服务器命令响应格式不正确。
MongoDB\Exception\UnsupportedException
如果使用了所选服务器不支持的功能(例如 collation
,readConcern
,writeConcern
)。
MongoDB\Exception\InvalidArgumentException
与参数或选项解析相关的错误。
MongoDB\Driver\Exception\RuntimeException 对于其他扩展级别的错误(例如连接错误)。
行为
When evaluating query criteria, MongoDB compares types and values according to its own comparison rules for BSON types, which differs from PHP's comparison and type juggling rules. When matching a special BSON type the query criteria should use the respective BSON class in the extension (e.g. use MongoDB\BSON\ObjectId to match an ObjectId).
示例
以下示例在 test
数据库中的 restaurants
集合中查找并删除具有 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" } }