Docs Menu
Docs Home
/ / /
Java Sync Driver
/ / /

Retrieve Data

On this page

  • Overview
  • Find Operation
  • Aggregate Operation

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

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 }

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 about how to specify a query, see our Specify a Query guide.

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:

Bson filter = Filters.and(Filters.gt("qty", 3), Filters.lt("qty", 9));
// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

For more information about how to build filters, see our Filters Builders guide.

The following shows the output of the preceding query:

{ "_id": 2, "color": "green", "qty": 8 }
{ "_id": 3, "color": "purple", "qty": 4 }

After the owner runs this query, they find two orders that matched the criteria.

For a runnable find() example, see our Find Multiple Documents page.

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.

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 collection

  • Groups orders by colors

  • Sums up the quantity field by color

  • Orders the results by highest-to-lowest quantity

Bson filter = Filters.empty();
// Prints the collection's "color" values and each value's frequency in descending frequency order
collection.aggregate(Arrays.asList(
Aggregates.match(filter),
Aggregates.group("$color", Accumulators.sum("qty", "$qty")),
Aggregates.sort(Sorts.descending("qty"))))
.forEach(doc -> System.out.println(doc.toJson()));

The following shows the output of the preceding aggregation:

{ "_id": "green", "qty": 19 }
{ "_id": "purple", "qty": 14 }

After the owner runs the aggregation, they find that "green" is the most purchased color.

For more information about 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:

← Read Operations