文档菜单
文档首页
/ / /
PHP库手册
/

指定返回的文档

在本页

  • 概述
  • 示例数据
  • 限制
  • 排序
  • 跳过
  • 组合限制、排序和跳过
  • 指定返回类型
  • 示例
  • 更多信息
  • API文档

在本指南中,您可以了解如何通过将以下选项传递给MongoDB\Collection::find()MongoDB\Collection::findOne() 方法来指定从读取操作中返回哪些文档和哪些类型

  • limit:指定从查询中返回的最大文档数

  • sort:指定返回文档的排序顺序

  • skip:指定在返回查询结果之前要跳过的文档数量

  • typeMap:将返回的文档转换为指定的数据类型

本指南中的示例使用来自Atlas 示例数据集sample_restaurants 数据库中的 restaurants 集合。要从您的 PHP 应用程序访问此集合,实例化一个连接到 Atlas 集群的 MongoDB\Client,并将以下值分配给您的 $collection 变量

$collection = $client->sample_restaurants->restaurants;

要了解如何创建免费的 MongoDB Atlas 集群并加载数据集,请参阅Atlas 入门指南。

要指定从读取操作返回的最大文档数,创建一个设置 limit 选项的数组,并将该数组作为参数传递给 MongoDB\Collection::find() 方法。

以下示例查找所有 cuisine 字段值为 'Italian' 的餐厅,并将结果限制为 5 个文档

$cursor = $collection->find(
['cuisine' => 'Italian'],
['limit' => 5]
);
foreach ($cursor as $doc) {
echo json_encode($doc), PHP_EOL;
}
{"_id":{"$oid":"..."},...,"name":"Isle Of Capri Resturant","restaurant_id":"40364373"}
{"_id":{"$oid":"..."},...,"name":"Marchis Restaurant","restaurant_id":"40364668"}
{"_id":{"$oid":"..."},...,"name":"Crystal Room","restaurant_id":"40365013"}
{"_id":{"$oid":"..."},...,"name":"Forlinis Restaurant","restaurant_id":"40365098"}
{"_id":{"$oid":"..."},...,"name":"Angelo Of Mulberry St.","restaurant_id":"40365293"}

技巧

前一个示例根据数据库中的自然顺序返回查询匹配的前五个文档。以下部分将描述如何以指定顺序返回文档。

要按指定顺序返回文档,创建一个设置sort选项的数组。设置此选项时,包括要按其排序的结果的字段和排序方向。值为1从最低到最高排序值,值为-1从最高到最低排序。然后,将数组作为参数传递给MongoDB\Collection::find()MongoDB\Collection::findOne()方法。

以下示例返回所有具有cuisine值为'Italian'的文档,并按name字段值的升序排序。

$cursor = $collection->find(
['cuisine' => 'Italian'],
['sort' => ['name' => 1]]
);
foreach ($cursor as $doc) {
echo json_encode($doc), PHP_EOL;
}
{"_id":{"$oid":"..."},...,"name":"44 Sw Ristorante & Bar","restaurant_id":"40698807"}
{"_id":{"$oid":"..."},...,"name":"900 Park","restaurant_id":"41707964"}
{"_id":{"$oid":"..."},...,"name":"A Voce","restaurant_id":"41434084"}
...
{"_id":{"$oid":"..."},...,"name":"Zucchero E Pomodori","restaurant_id":"41189590" }

要跳过指定数量的文档,在返回查询结果之前,创建一个设置skip选项的数组,并将该数组作为参数传递给MongoDB\Collection::find()MongoDB\Collection::findOne()方法。

以下示例返回所有具有borough字段值为'Manhattan'的文档,并跳过前10个文档。

$cursor = $collection->find(
['borough' => 'Manhattan'],
['skip' => 10]
);
foreach ($cursor as $doc) {
echo json_encode($doc), PHP_EOL;
}
{"_id":{"$oid":"..."},...,"name":"Cafe Metro","restaurant_id":"40363298"}
{"_id":{"$oid":"..."},...,"name":"Lexler Deli","restaurant_id":"40363426"}
{"_id":{"$oid":"..."},...,"name":"Domino'S Pizza","restaurant_id":"40363644"}
...

您可以在单个选项数组中设置 limitsortskip 选项,并将该数组作为参数传递给读取操作。这允许您设置要返回的最大排序文档数,在返回之前跳过指定数量的文档。

以下示例返回具有 cuisine 值为 '意大利'5 个文档。结果按 name 字段值的升序排序,跳过了前 10 个文档。

$options = [
'sort' => ['name' => 1],
'limit' => 5,
'skip' => 10,
];
$cursor = $collection->find(['cuisine' => 'Italian'], $options);
foreach ($cursor as $doc) {
echo json_encode($doc), PHP_EOL;
}
{"_id":{"$oid":"..."},...,"name":"Acqua","restaurant_id":"40871070"}
{"_id":{"$oid":"..."},...,"name":"Acqua Restaurant","restaurant_id":"41591488"}
{"_id":{"$oid":"..."},...,"name":"Acqua Santa","restaurant_id":"40735858"}
{"_id":{"$oid":"..."},...,"name":"Acquista Trattoria","restaurant_id":"40813992"}
{"_id":{"$oid":"..."},...,"name":"Acquolina Catering","restaurant_id":"41381423"}

注意

调用这些方法的顺序不会改变返回的文档。MongoDB PHP 库会自动重新排序调用,首先执行排序操作,然后是跳过操作,最后是限制操作。

要自定义读取操作返回的文档的数据类型,您可以在数组参数中传递 typeMap 选项。

默认情况下,在 MongoDB\ClientMongoDB\DatabaseMongoDB\Collection 实例上调用的方法使用以下类型映射

[
'array' => 'MongoDB\Model\BSONArray',
'document' => 'MongoDB\Model\BSONDocument',
'root' => 'MongoDB\Model\BSONDocument',
]

此默认类型映射执行以下转换

  • 数组转换为 MongoDB\Model\BSONArray 对象

  • 顶级和嵌套的 BSON 文档转换为 MongoDB\Model\BSONDocument 对象

在自定义类型映射中,您可以指定转换为任何实现 MongoDB\BSON\Unserializable 的类型,以及 arraystdClassobject 类型。

以下示例返回所有具有 cuisine 值为 'Hawaiian' 的文档,并指定 typeMap 选项将文档转换为数组值

$options = [
'typeMap' => [
'root' => 'array',
'document' => 'array'
]
];
$cursor = $collection->find(['cuisine' => 'Hawaiian'], $options);
foreach ($cursor as $doc) {
print_r($doc) . PHP_EOL;
}
Array
(
[_id] => MongoDB\BSON\ObjectId Object
(
[oid] => ...
)
[address] => Array
(
...
)
[borough] => Manhattan
[cuisine] => Hawaiian
[grades] => Array
(
...
)
[name] => Makana
[restaurant_id] => 41509012
)
...

有关获取文档的更多信息,请参阅检索数据指南。

有关指定查询的更多信息,请参阅指定查询指南。

有关本指南中讨论的任何方法或类型的更多信息,请参阅以下 API 文档

返回

计算文档数量