Overview
在本指南中,您可以学习;了解如何使用Kotlin驾驶员通过读取操作从MongoDB集合中检索数据。您可以调用 find() 方法来检索与查询过滤中指定的一设立条件匹配的文档。
样本数据
The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started guide.
以下Kotlin数据类对此集合中的文档进行建模:
data class Movie(val title: String, val runtime: Int, val imdb: IMDB){ data class IMDB(val rating: Double) }
以下Kotlin数据类对本指南中示例返回的结果进行建模:
data class Results(val title: String)
查找操作
find() 方法从集合中检索文档。此方法采用查询筛选条件并返回所有匹配的文档。查询筛选条件是一个文档,其中指定了驱动程序用于匹配集合中的文档的条件。
要学习;了解有关查询筛选器的更多信息,请参阅《 指定查询》指南。
查找一个文档示例
以下示例将 firstOrNull() 方法链接到 find() 方法调用,以查找 name字段的值为 "The Room" 的第一个文档:
val projectionFields= Projections.fields( Projections.include(Movie::title.name, Movie::imdb.name), Projections.excludeId() ) val resultsFlow = collection.withDocumentClass<Results>() .find(eq(Movie::title.name, "The Room")) .projection(projectionFields) .firstOrNull() if (resultsFlow == null) { println("No results found."); } else { println(resultsFlow) }
Movie(title=The Room, runtime=99, imdb=IMDB(rating=3.7))
查找文档示例
此示例使用 find() 方法查找其中 runtime字段的值小于 15 的所有文档。 find() 操作返回一个 FindFlow对象,您可以使用 collect() 方法遍历该对象,如以下示例所示:
val projectionFields= Projections.fields( Projections.include(Movie::title.name, Movie::imdb.name), Projections.excludeId() ) val resultsFlow = collection.withDocumentClass<Results>() .find(lt(Movie::runtime.name, 15)) .projection(projectionFields) resultsFlow.collect { println(it) }
Movie(title=Meshes of the Afternoon, runtime=14, imdb=IMDB(rating=8.0)) Movie(title=The Band Concert, runtime=9, imdb=IMDB(rating=7.9)) Movie(title=What's Opera, Doc?, runtime=7, imdb=IMDB(rating=8.3)) ...
注意
查找所有文档
要查找集合中的所有文档,请将空筛选器传递给find()方法:
val resultsFlow = collection.withDocumentClass<Results>() .find()
修改查找行为
您可以通过将方法链接到 FindFlow实例来修改 find() 方法的行为。下表描述了修改查询的常用方法:
方法 | 说明 |
|---|---|
| Limits the number of documents to return per batch. To learn more about
batch size, see cursor.batchSize()
in the MongoDB Server manual. |
| Sets the collation options for the query. |
| Processes each element in the flow and applies the given lambda. |
| Specifies a string to attach to the query. This can help you trace and interpret the
operation in the server logs and in profile data. |
| Returns the first document that matches the query or throws a MongoClientException
if no matching documents exist. |
| Returns the first document that matches the query or null if no matching documents exist. |
| Specifies the index to use for the query. |
| Limits the number of documents to be returned from the query. |
| Sets the number of documents to skip. |
| Defines the sort criteria to apply to the query. |
以下示例链接了 limit() 方法,将查询返回的文档数量限制为 10:
val projectionFields= Projections.fields( Projections.include(Movie::title.name, Movie::imdb.name), Projections.excludeId() ) val resultsFlow = collection.withDocumentClass<Results>() .find() .projection(projectionFields) .limit(10)
有关修改find() 行为的方法的完整列表,请参阅Kotlin驾驶员API文档中的 FindFlow 类。
更多信息
如需了解有关查询过滤器的更多信息,请参阅指定查询。
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: