Overview
在本指南中,您可以学习;了解如何使用Ruby驾驶员通过读取操作从MongoDB集合中检索数据。您可以对集合调用 find
方法,以检索与一设立条件匹配的文档。
样本数据
本指南中的示例使用 Atlas示例数据集的 sample_training
数据库中的 companies
集合。要从Ruby应用程序访问权限此集合,请创建一个连接到Atlas 集群的Mongo::Client
对象,并将以下值分配给 database
和 collection
变量:
database = client.use('sample_training') collection = database[:companies]
要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。
查找文档
要从集合中检索文档,请使用 find
方法。该方法接受一个查询过滤参数并返回一个表示该查询的Mongo::Collection::View
对象。驾驶员会推迟查询执行,直到您使用 first
或 each
等方法获取结果。请求结果后,驾驶员会将查询发送到服务器并返回一个 Mongo::Cursor
对象,您可以通过该对象访问权限结果。
您可以将选项方法链接到 find
方法以优化操作结果。
提示
要学习;了解有关查询筛选器的更多信息,请参阅《 指定查询》指南。
查找多个文档
要查找集合中的多个文档,请将查询筛选器传递给 find
方法,其中指定要检索的文档条件。
以下示例使用find
方法查找founded_year
字段值为1970
的所有文档:
results = collection.find(founded_year: 1970)
当您对表示查询的 Mongo::Collection::View
对象调用 each
方法时,驾驶员会返回一个 Mongo::Cursor
对象。游标是一种机制,允许应用程序迭代数据库结果,同时在给定时间仅在内存中保存其中的子集。当 find
方法返回大量文档时,游标非常有用。
以下代码调用 each
方法来遍历查询结果:
results.each do |doc| puts doc end
{"_id"=>BSON::ObjectId('...'), "name"=>"Mitsubishi Motors", "permalink"=>"mitsubishi-motors", "crunchbase_url"=>"http://www.crunchbase.com/company/mitsubishi-motors", "homepage_url"=>"http://www.mitsubishi-motors.com", ...} {"_id"=>BSON::ObjectId('...'), "name"=>"Western Digital", "permalink"=>"western-digital", "crunchbase_url"=>"http://www.crunchbase.com/company/western-digital", "homepage_url"=>"http://www.wdc.com/en", ...} {"_id"=>BSON::ObjectId('...'), "name"=>"Celarayn", "permalink"=>"celarayn", "crunchbase_url"=>"http://www.crunchbase.com/company/celarayn", "homepage_url"=>"http://www.celarayn.es", ...}
注意
查找所有文档
要查找集合中的所有文档,请调用 find
方法而不传递查询过滤:
results = collection.find
查找一个文档
要查找集合中的单个文档,请调用 find
方法并传递查询过滤,其中指定要查找的文档条件。 然后,将 first
方法链接到 find
。
如果查询过滤匹配多个文档,则 first
方法将从操作结果中检索第一个匹配的文档。
以下示例将 first
方法链接到 find
,以查找 name
字段值为 'LinkedIn'
的第一个文档:
document = collection.find(name: 'LinkedIn').first puts document
{"_id"=>BSON::ObjectId('...'), "name"=>"LinkedIn", "permalink"=>"linkedin", "crunchbase_url"=>"http://www.crunchbase.com/company/linkedin", "homepage_url"=>"http://linkedin.com", "blog_url"=>"http://blog.linkedin.com", ...}
修改查找行为
您可以将选项方法链接到 find
方法来修改操作结果。下表描述了其中一些选项:
选项 | 说明 |
---|---|
| The number of documents to return per batch. The default value is 101 .Type: Integer |
| The collation to use for the operation. The default value is the collation
specified for the collection. Type: Hash |
| The comment to attach to the operation. Type: Object |
| 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: Hash |
以下示例使用find
方法查找number_of_employees
字段值为1000
的所有文档。 该示例使用limit
选项返回最多2
结果:
limit_results = collection.find(number_of_employees: 1000).limit(2) limit_results.each do |doc| puts doc end
{"_id"=>BSON::ObjectId('...'), "name"=>"Akamai Technologies", "permalink"=>"akamai-technologies", "crunchbase_url"=>"http://www.crunchbase.com/company/akamai-technologies", "homepage_url"=>"http://www.akamai.com", ...} {"_id"=>BSON::ObjectId('...'), "name"=>"Yodle", "permalink"=>"yodle", "crunchbase_url"=>"http://www.crunchbase.com/company/yodle", "homepage_url"=>"http://www.yodle.com", ...}
有关选项的完整列表,请参阅 find 方法的API文档。
更多信息
如需学习;了解有关查询筛选器的更多信息,请参阅“指定查询”指南。
要查看使用Ruby驾驶员检索文档的代码示例,请参阅从MongoDB读取数据。
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: