Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

MongoDB Search

In this guide, you can learn how to use the Java driver to run MongoDB Search queries on a collection. MongoDB Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. MongoDB Search indexes specify the behavior of the search and which fields to index.

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 deployment and load the sample datasets, see the MongoDB Get Started guide. To learn more about aggregation operations and builders, see the Aggregation guide.

This section shows how to create an aggregation pipeline to run a MongoDB Search query on a collection. You can use the Aggregates.search() builder method to create a $search pipeline stage, which specifies the search criteria. Then, call the aggregate() method and pass your pipeline as a parameter.

Before running a MongoDB Search query, you must create a MongoDB Search index on your collection. To learn how to programmatically create a MongoDB Search index, see the MongoDB Search and Vector Search Indexes section in the Indexes guide.

This example runs a MongoDB Search query by performing the following actions:

  • Constructs a $search stage by using the Aggregates.search() builder method, instructing the driver to query for documents in which the title field contains the word "Alabama"

  • Constructs a $project stage by using the Aggregates.project() builder method, instructing the driver to include the title field in the query results

  • Passes the pipeline stages to the aggregate() method and prints the results

collection.aggregate(
Arrays.asList(
Aggregates.search(SearchOperator.text(
SearchPath.fieldPath("title"), "Alabama")),
Aggregates.project(Projections.include("title"))
)
).forEach(doc -> System.out.println(doc.toJson()));
{"_id": {"$oid": "..."}, "title": "Alabama Moon"}
{"_id": {"$oid": "..."}, "title": "Crazy in Alabama"}
{"_id": {"$oid": "..."}, "title": "Sweet Home Alabama"}

Tip

Java Driver MongoDB Search Examples

To view more examples that use the Java driver to perform MongoDB Search queries, see MongoDB Search Tutorials in the Atlas documentation.

Use the searchMeta() method to create a $searchMeta pipeline stage, which returns only the metadata from of the MongoDB Search results.

Note

Atlas and Community Edition Version Requirements

This aggregation pipeline operator is available only on MongoDB Atlas clusters running v4.4.11 and later, or on MongoDB Community Edition clusters running MongoDB v8.2 or later. For a detailed list of version availability, see the MongoDB Atlas documentation on $searchMeta.

The following example shows the near metadata for a MongoDB Search aggregation stage:

Aggregates.searchMeta(
SearchOperator.near(2010, 1, fieldPath("year")));

To learn more about this helper method, see the searchMeta() API documentation.

To learn more about MongoDB Search, see MongoDB Search in the Atlas documentation.

To learn more about the methods mentioned in this guide, see the following API documentation:

Back

Run a Command

On this page