对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB\GridFS\Bucket::find()

MongoDB\GridFS\Bucket::find()

从 GridFS 存储桶的文件collection中查找与查询匹配的文档。

function find(
array|object $filter = [],
array $options = []
): MongoDB\Driver\Cursor
$filter : array|object
指定要查询的文档的筛选条件。
$options : array

指定所需选项的数组。

名称
类型
说明

allowDiskUse

布尔

允许写入临时文件。设置为 true 时,查询可以将数据写入到 dbPath 目录的 _tmp 子目录中。

allowPartialResults

布尔

For queries against a sharded collection, returns partial results from the mongos if some shards are unavailable instead of throwing an error.

batchSize

整型

第一批中要返回的文档数量。默认值为 101。batchSize 为 0 是指将建立游标,但第一批不会返回任何文档。

Unlike the previous wire protocol version, a batchSize of 1 for the find command does not close the cursor.

编解码器

MongoDB\Codec\DocumentCodec

使用类型编解码器对数据进行编码,用于对文档进行编码或解码。此选项与 typeMap 选项互斥。

默认为存储桶的编解码器。 默认codec选项的继承优先于typeMap选项的继承。

1.17版本新增。

排序规则

数组|对象

Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.

comment

混合

使用户可以指定任意注释来帮助通过数据库分析器、currentOp 输出和日志来跟踪操作。

评论可以是从MongoDB 4.4 开始的任何有效BSON类型。早期服务器版本仅支持字符串值。

cursorType

整型

表示要使用的游标类型。cursorType 支持以下值:

  • MongoDB\Operation\Find::NON_TAILABLE (默认)

  • MongoDB\Operation\Find::TAILABLE

提示

字符串|数组|对象

要使用的索引。将索引名称指定为字符串或将索引键模式指定为文档。如果指定,查询系统将只考虑使用提示索引的计划。

1.2版本新增。

let

数组|对象

参数名称和值的映射。值必须是常量或不引用文档字段的封闭表达式。然后可以在聚合表达式上下文中将参数作为变量进行访问(例如 $$var)。

5.0 之前的服务器版本不支持此功能,如果使用,将导致执行时出现异常。

1.13版本新增。

limit

整型

要返回的最大文档数。如果未指定,则默认为无限制。限制为 0 相当于不设限制。

负限值与正限值相似,但在返回单个批次结果后将关闭游标。因此,对于负限值,如果有限的结果集不适合单个批次,则收到的文档数量将小于指定的限值。通过传递负限值,客户端可向服务器表明它不会通过 getMore 请求后续批次。

Max

数组|对象

特定索引的独占上限。

1.2版本新增。

maxAwaitTimeMS

整型

正整数,表示服务器在没有可用数据时阻止 getMore 操作的时间限制(以毫秒为单位)。仅当 cursorType 为 TAILABLE_AWAIT 时才应使用此选项。

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.

noCursorTimeout

布尔

防止服务器在闲置期(10 分钟)后使空闲游标超时。

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

数组|对象

对结果排序的排序规范。

snapshot

布尔

防止游标由于介入的写入操作而多次返回文档。

自版本 1.4 起已弃用。

1.2版本新增。

typeMap

阵列

应用游标的类型映射,决定如何将BSON文档转换为PHP值。默认为存储桶的类型映射。

MongoDB\ 驱动程序\Cursor对象。

MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。

MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。

MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。

在评估查询条件时, MongoDB会根据自己的BSON 类型比较规则来比较类型和值,这与 PHP 的比较和类型规则不同。匹配特殊BSON类型时,查询条件应使用扩展中相应的BSON类(例如使用MongoDB\BSON\ObjectId来匹配ObjectId)。

<?php
$bucket = (new MongoDB\Client)->test->selectGridFSBucket();
$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);
$bucket->uploadFromStream('b', $stream);
$cursor = $bucket->find(
['length' => ['$lte' => 6]],
[
'projection' => [
'filename' => 1,
'length' => 1,
'_id' => 0,
],
'sort' => ['length' => -1],
]
);
var_dump($cursor->toArray());

而输出将类似如下所示:

array(1) {
[0]=>
object(MongoDB\Model\BSONDocument)#3015 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["filename"]=>
string(1) "b"
["length"]=>
int(6)
}
}
}