MongoDB\Collection::findOneAndReplace()
定义
参数
$filter
: array|object- 指定要替换的文档的过滤条件。
$replacement
: array|object- 替换文档。
$options
: array指定所需选项的数组。
名称类型描述bypassDocumentValidation布尔型如果为true
,允许写操作绕过文档级别验证。默认为false
。codecMongoDB\Codec\DocumentCodeccollationarray|object注释混合提示字符串|数组|对象要使用的索引。指定索引名称作为字符串或索引键模式作为文档。如果指定,则查询系统将仅考虑使用提示索引的计划。
此选项自MongoDB 4.4起可用,如果为旧服务器版本指定,则在执行时将引发异常。
新功能版本1.7.
letarray|object参数名称和值的映射。值必须是常量或封闭表达式,不能引用文档字段。参数然后可以作为聚合表达式上下文中的变量访问(例如,
$$var
)。此功能不支持旧于5.0的服务器版本,如果使用则会在执行时引发异常。
新功能版本1.13.
maxTimeMS整数处理游标上的操作的总时间限制(以毫秒为单位)。MongoDB将在最早的中断点后终止操作。
投影array|objectreturnDocument整数指定是否在应用替换前或应用替换后返回文档。
returnDocument
支持以下值MongoDB\Operation\FindOneAndReplace::RETURN_DOCUMENT_BEFORE
(默认值)MongoDB\Operation\FindOneAndReplace::RETURN_DOCUMENT_AFTER
会话与操作关联的客户端会话。
新功能版本1.3.
排序array|object结果的排序规范。typeMap数组将类型映射应用于游标,这决定了BSON文档如何转换为PHP值。默认为集合的类型映射。
这将用于返回的结果文档。
upsert布尔型如果设置为true
,当没有文档匹配查询条件时创建新文档。默认值是false
,表示当没有找到匹配项时不会插入新文档。writeConcern
返回值
一个数组对象,表示原始文档或替换的文档,具体取决于 returnDocument
选项指定的值。默认情况下,返回原始文档。如果没有文档与查询匹配,则返回 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).
示例
以下示例在restaurants
集合和test
数据库中
{ "_id" : ObjectId("576023c7b02fa9281da4139e"), "address" : { "building" : "977", "coord" : [ -74.06940569999999, 40.6188443 ], "street" : "Bay Street", "zipcode" : "10305" }, "borough" : "Staten Island", "cuisine" : "French", "grades" : [ { "date" : ISODate("2014-08-15T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2014-02-13T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-06-07T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Zest", "restaurant_id" : "41220906" }
以下操作用新文档替换具有restaurant_id
为"41220906"
的文档
$collection = (new MongoDB\Client)->teset->restaurants; $replacedRestaurant = $collection->findOneAndReplace( [ 'restaurant_id' => '41220906' ], [ 'Borough' => 'Staten Island', 'cuisine' => 'Italian', 'grades' => [], 'name' => 'Staten Island Pastaria', 'restaurant_id' => '999999999', ], [ 'returnDocument' => MongoDB\Operation\FindOneAndReplace::RETURN_DOCUMENT_AFTER ] ); var_dump($replacedRestaurant);
输出结果将类似
object(MongoDB\Model\BSONDocument)#18 (1) { ["storage":"ArrayObject":private]=> array(6) { ["_id"]=> object(MongoDB\BSON\ObjectId)#11 (1) { ["oid"]=> string(24) "594d5ef380a846852a4b5837" } ["Borough"]=> string(13) "Staten Island" ["cuisine"]=> string(7) "Italian" ["grades"]=> object(MongoDB\Model\BSONArray)#17 (1) { ["storage":"ArrayObject":private]=> array(0) { } } ["name"]=> string(22) "Staten Island Pastaria" ["restaurant_id"]=> string(9) "999999999" } }