Retrieve Data
On this page
Overview
In this guide, you can learn how to retrieve data from your MongoDB database. To retrieve data, use read operations.
Read operations allow you to do the following:
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 feature examples of how the owner of a paint
store manages their customers' orders. For each order, the owner keeps
track of the color and quantity, which corresponds to the color
and
qty
fields in their paint_order
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 with 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 our Specify a Query guide.
Example
The owner would like to know which orders contain greater than three, but less than nine cans of paint from their paint_order collection.
To address this scenario, the owner finds 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)
After the owner runs this query, they find two orders that matched the criteria.
For more information on how to build filters, see our Filters Builders guide.
For a runnable find()
example, see our Find Multiple
Documents page.
Aggregate Operation
Use the aggregate operation to perform the stages in an aggregation pipeline. An aggregation pipeline is a multi-staged transformation that produces an aggregated result.
To perform an aggregate operation, call the aggregate()
method on an
instance of a 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. For more information, see our
Aggregation guide.
Example
The owner would like to know which paint color is the most purchased (highest quantity sold) from their paint_order collection.
To address the scenario, the owner creates an aggregation pipeline that:
Matches all the documents in the
paint_order
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=8, color=green) PaintOrder(id=3, qty=4, color=purple)
After the owner runs the aggregation, they find that "green" is the most purchased color.
For more information on how to construct an aggregation pipeline, see the MongoDB server manual page on Aggregation.
For additional information on the methods mentioned on this page, see the following API Documentation: