Retrieve Data
On this page
Overview
In this guide, you can learn how to retrieve data from your MongoDB database by using the Kotlin driver. You can perform read operations to retrieve data from MongoDB.
Read operations enable you to perform the following tasks:
Retrieve a subset of documents from your collection using a find operation
Perform transformations on retrieved documents from your collection using an aggregate operation
Monitor real-time changes to your database using change streams
Sample Data for Examples
The following sections include examples that demonstrate how you can
manage customer orders for cans of paint. For each order, you keep
track of the color and quantity, which corresponds to the color
and
qty
fields in documents in the orders
collection:
{ "_id": 1, "color": "purple", "qty": 10 } { "_id": 2, "color": "green", "qty": 8 } { "_id": 3, "color": "purple", "qty": 4 } { "_id": 4, "color": "green", "qty": 11 }
This data is modeled by the following Kotlin data class:
data class PaintOrder( val id: Int, val qty: Int, val color: String )
Find Operation
Use the find operation to retrieve a subset of your existing data in MongoDB. You can specify what data to return including which documents to retrieve, in what order to retrieve them, and how many to retrieve.
To perform a find operation, call the find()
method on an instance
of a MongoCollection
. This method searches a collection for documents that
match the query filter you provide. For more information on how to
specify a query, see the Specify a Query guide.
Example
You want to know which orders contain greater than 3
, but
less than 9
cans of paint.
Run the following code to find orders to match the criteria:
val filter = Filters.and(Filters.gt("qty", 3), Filters.lt("qty", 9)) val resultsFlow = collection.find(filter) resultsFlow.collect { println(it) }
PaintOrder(id=2, qty=8, color=green) PaintOrder(id=3, qty=4, color=purple)
To learn more about the Filters
builder, see the
Filters Builders guide.
To view a runnable find()
example, see the Find Multiple Documents
usage example.
Aggregate Operation
Use aggregation operations to run an aggregation pipeline on your data. An aggregation pipeline is a multi-staged transformation that produces an aggregated result.
To perform an aggregation operation, call the aggregate()
method on an
instance of MongoCollection
. This method accepts aggregation
expressions to run in sequence. To perform aggregations, you can
define aggregation stages that specify how to match documents, rename
fields, and group values. To learn more, see the
Aggregation guide.
Example
You want to know which paint color is the most popular by finding the color that is bought the most.
You can create an aggregation pipeline that performs the following actions:
Matches all the documents in the
orders
collectionGroups orders by colors
Sums up the quantity field by color
Orders the results by highest-to-lowest quantity
data class AggregationResult( val id: String, val qty: Int) val filter = Filters.empty() val pipeline = listOf( Aggregates.match(filter), Aggregates.group( "\$color", Accumulators.sum("qty", "\$qty") ), Aggregates.sort(Sorts.descending("qty")) ) val resultsFlow = collection.aggregate<AggregationResult>(pipeline) resultsFlow.collect { println(it) }
PaintOrder(id=2, qty=19, color=green) PaintOrder(id=3, qty=14, color=purple)
To learn more about constructing aggregation pipelines, see Aggregation in the Server manual.
To learn more about the methods mentioned on this page, see the following API Documentation: