Overview
在本指南中,您可以学习;了解如何使用MongoDB PHP库通过读取操作从MongoDB集合中检索数据。 您可以对集合调用 MongoDB\Collection::find()
或MongoDB\Collection::findOne()
方法,以检索与一设立条件匹配的文档。
样本数据
本指南中的示例使用 Atlas示例数据集的sample_training
数据库中的companies
集合。 要从PHP应用程序访问权限此集合,请实例化一个连接到Atlas 集群的MongoDB\Client
,并将以下值分配给$collection
变量:
$collection = $client->sample_training->companies;
要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。
查找文档
MongoDB PHP库包括两种从集合中检索文档的方法: MongoDB\Collection::findOne()
和MongoDB\Collection::find()
。 这些方法采用查询过滤并返回一个或多个匹配的文档。 查询过滤指定驾驶员用于检索查询中的文档的搜索条件。
提示
要学习;了解有关查询筛选器的更多信息,请参阅《 指定查询》指南。
查找一个文档
要查找集合中的单个文档,请调用MongoDB\Collection::findOne()
方法并传递查询过滤,其中指定要查找的文档条件。
findOne()
方法返回array
、 object
或null
值。 如果查询过滤与文档匹配,该方法将返回包含该文档的array|object
实例。 返回类型取决于typeMap
选项的值。 如果查询过滤与任何文档均不匹配,该方法将返回null
。
提示
要学习;了解有关findOne()
选项(例如typeMap
)的更多信息,请参阅本指南的“修改查找行为”部分。
如果查询过滤匹配多个文档,则findOne()
方法会从检索到的结果中返回第一个匹配的文档。
以下示例使用findOne()
方法查找name
字段值为'LinkedIn'
的第一个文档:
$document = $collection->findOne(['name' => 'LinkedIn']); echo json_encode($document), PHP_EOL;
{"_id":{"$oid":"..."},"name":"LinkedIn","permalink":"linkedin","crunchbase_url": "https:\/\/siteproxy.ruqli.workers.dev:443\/http\/www.crunchbase.com\/company\/linkedin","homepage_url":"https:\/\/siteproxy.ruqli.workers.dev:443\/http\/linkedin.com", ... }
查找多个文档
要查找集合中的多个文档,请将查询筛选器传递给 MongoDB\Collection::find()
方法,其中指定要检索的文档条件。
以下示例使用find()
方法查找founded_year
字段值为1970
的所有文档:
$results = $collection->find(['founded_year' => 1970]);
find()
方法返回一个MongoDB\Driver\Cursor
实例,您可以对其进行迭代以查看匹配的文档。 游标是一种机制,允许应用程序迭代数据库结果,同时在给定时间仅在内存中保存其中的子集。 当find()
方法返回大量文档时,游标非常有用。
您可以使用foreach
循环遍历游标中的文档,如以下示例所示:
foreach ($results as $doc) { echo json_encode($doc), PHP_EOL; }
{"_id":{"$oid":"..."},"name":"Mitsubishi Motors","permalink":"mitsubishi-motors", "crunchbase_url":"https:\/\/siteproxy.ruqli.workers.dev:443\/http\/www.crunchbase.com\/company\/mitsubishi-motors", ... } {"_id":{"$oid":"..."},"name":"Western Digital","permalink":"western-digital", "crunchbase_url":"https:\/\/siteproxy.ruqli.workers.dev:443\/http\/www.crunchbase.com\/company\/western-digital", ... } {"_id":{"$oid":"..."},"name":"Celarayn","permalink":"celarayn","crunchbase_url": "https:\/\/siteproxy.ruqli.workers.dev:443\/http\/www.crunchbase.com\/company\/celarayn", ... }
注意
查找所有文档
要查找集合中的所有文档,请将空筛选器传递给find()
方法:
$cursor = $collection->find([]);
修改查找行为
您可以通过将指定选项值的大量作为参数传递来修改MongoDB\Collection::find()
和MongoDB\Collection::findOne()
方法的行为。 下表描述了您可以在大量中设立的一些选项:
选项 | 说明 |
---|---|
| The maximum number of documents within each batch returned in a query result. By default,
the find command has an initial batch size of 101 documents
and a maximum size of 16 mebibytes (MiB) for each subsequent batch. This
option can enforce a smaller limit than 16 MiB, but not a larger
one. If you set batchSize to a limit that results in batches larger than
16 MiB, this option has no effect.Type: integer |
| The collation to use for the operation. The default value is the collation
specified for the collection. To learn more, see the Collation
section of this page. Type: array|object |
| The comment to attach to the operation. Type: any BSON type |
| The type of cursor to use for the operation. The default value is
MongoDB\Operation\Find::NON_TAILABLE .Type: MongoDB\Operation\Find |
| The maximum number of documents the operation can return. Type: integer |
| The number of documents to skip before returning results. Type: integer |
| The order in which the operation returns matching documents. Type: array|object |
| The type map to apply to cursors, which determines how BSON documents
are converted to PHP values. The default value is the collection's type map. Type: array |
以下示例使用find()
方法查找number_of_employees
字段值为1000
的所有文档。 该示例使用limit
选项返回最多5
结果:
$results = $collection->find( ['number_of_employees' => 1000], ['limit' => 5], ); foreach ($results as $doc) { echo json_encode($doc), PHP_EOL; }
有关选项的完整列表,请参阅findOne()和find()参数的API文档。
排序规则
要为操作指定排序规则,请传递 $options
大量参数,该参数将 collation
选项设置为操作方法。将 collation
选项分配给配置排序规则规则的大量。
下表描述了可以设立以配置排序规则的字段:
字段 | 说明 |
---|---|
| (Required) Specifies the International Components for Unicode (ICU) locale. For a
list of supported locales, see Collation Locales and Default Parameters
in the MongoDB Server manual. Data Type: string |
| (Optional) Specifies whether to include case comparison. When set to true , the comparison behavior depends on the value of
the strength field:- If strength is 1 , the PHP library compares basecharacters and case. - If strength is 2 , the PHP library compares basecharacters, diacritics, other secondary differences, and case. - If strength is any other value, this field is ignored.When set to false , the PHP library doesn't include case comparison at
strength level 1 or 2 .Data Type: bool Default: false |
| (Optional) Specifies the sort order of case differences during tertiary
level comparisons. Data Type: string Default: "off" |
| (Optional) Specifies the level of comparison to perform, as defined in the
ICU documentation. Data Type: int Default: 3 |
| (Optional) Specifies whether the driver compares numeric strings as numbers. If set to true , the PHP library compares numeric strings as numbers.
For example, when comparing the strings "10" and "2", the library uses the
strings' numeric values and treats "10" as greater than "2".If set to false , the PHP library compares numeric strings
as strings. For example, when comparing the strings "10" and "2", the library
compares one character at a time and treats "10" as less than "2".For more information, see Collation Restrictions
in the MongoDB Server manual. Data Type: bool Default: false |
| (Optional) Specifies whether the library considers whitespace and punctuation as base
characters for comparison purposes. Data Type: string Default: "non-ignorable" |
| (Optional) Specifies which characters the library considers ignorable when
the alternate field is set to "shifted" .Data Type: string Default: "punct" |
| (Optional) Specifies whether strings containing diacritics sort from the back of the string
to the front. Data Type: bool Default: false |
更多信息
如需学习;了解有关查询筛选器的更多信息,请参阅“指定查询”指南。
API 文档
要进一步了解本指南所讨论的任何方法,请参阅以下 API 文档: