定义
参数
$filter: array|object- 指定要查询的文档的筛选条件。
$options: array指定所需选项的数组。
名称类型说明allowDiskUse
布尔
允许写入临时文件。设置为
true时,查询可以将数据写入到dbPath目录的_tmp子目录中。allowPartialResults
布尔
For queries against a sharded collection, returns partial results from the
mongosif some shards are unavailable instead of throwing an error.编解码器
MongoDB\Codec\DocumentCodec
排序规则
数组|对象
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.comment
混合
提示
字符串|数组|对象
要使用的索引。将索引名称指定为字符串或将索引键模式指定为文档。如果指定,查询系统将只考虑使用提示索引的计划。
1.2版本新增。
let
数组|对象
参数名称和值的映射。值必须是常量或不引用文档字段的封闭表达式。然后可以在聚合表达式上下文中将参数作为变量进行访问(例如
$$var)。5.0 之前的服务器版本不支持此功能,如果使用,将导致执行时出现异常。
1.13版本新增。
Max
数组|对象
特定索引的独占上限。
1.2版本新增。
maxScan
整型
执行查询时要扫描的最大文档数或索引键数。
自版本 1.4 起已弃用。
1.2版本新增。
maxTimeMS
整型
The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
min
数组|对象
特定索引的包含下限。
1.2版本新增。
modifiers
数组|对象
Meta operators that modify the output or behavior of a query. Use of these operators is deprecated in favor of named options.
oplogReplay
布尔
针对副本集的内部用法。要使用
oplogReplay,您必须在筛选器中包含以下条件:{ ts: { $gte: <timestamp> } } MongoDB\ BSON \Timestamp 类引用描述了如何使用PHP表示 MongoDB 的BSON时间戳类型。
自版本 1.7 起已弃用。
投影
数组|对象
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.
事务外的
Read concern to use for the operation. Defaults to the bucket's read concern.
无法将单个操作的读关注(read concern)指定为ACID 事务的一部分。相反,请在启动ACID 事务时设立
readConcern选项。readPreference
Read preference to use for the operation. Defaults to the bucket's read preference.
returnKey
布尔
如果为 true,则只返回结果文档中的索引键。
1.2版本新增。
会话
与操作相关联的客户端会话。
1.3版本新增。
showRecordId
布尔
确定定是否返回每个文档的记录标识符。如果为 true,则在返回的文档中添加字段
$recordId。1.2版本新增。
跳过
整型
要跳过的文档数量。默认值为
0。sort
数组|对象
对结果排序的排序规范。
typeMap
阵列
应用游标的类型映射,决定如何将BSON文档转换为PHP值。默认为存储桶的类型映射。
这将用于返回的结果文档。
Return Values
An array or object for the first document that matched the query, or null if no document matched the query. The return type will depend on the typeMap option.
错误/异常
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
行为
在评估查询条件时, MongoDB会根据自己的BSON 类型比较规则来比较类型和值,这与 PHP 的比较和类型规则不同。匹配特殊BSON类型时,查询条件应使用扩展中相应的BSON类(例如使用MongoDB\BSON\ObjectId来匹配ObjectId)。
示例
$bucket = (new MongoDB\Client)->test->selectGridFSBucket(); $stream = fopen('php://temp', 'w+b'); fwrite($stream, "foobar"); rewind($stream); $bucket->uploadFromStream('b', $stream); $fileDocument = $bucket->findOne( ['length' => ['$lte' => 6]], [ 'projection' => [ 'filename' => 1, 'length' => 1, '_id' => 0, ], 'sort' => ['length' => -1], ] ); var_dump($fileDocument);
而输出将类似如下所示:
object(MongoDB\Model\BSONDocument)#3004 (1) { ["storage":"ArrayObject":private]=> array(2) { ["filename"]=> string(1) "b" ["length"]=> int(6) } }