Docs Menu
Docs Home
/
MongoDB for IntelliJ Plugin

Run Java Queries

On this page

  • Definition
  • Behavior
  • Example

The MongoDB for IntelliJ Plugin enables you to run queries written in Java directly in the Database Explorer Playgrounds.

The Run icon appears next to your MongoDB queries.

Click the Run icon to convert your Java query to mongosh syntax.

When you click the Run icon, the plugin automatically converts your Java query to mongosh syntax and opens a Playground file with the populated query.

For field values that are variables determined at runtime, the plugin creates a placeholder variable. You can populate this placeholder with a test value and run the query in the Playground.

In this example, the Java query on the production.trips collection resembles the following:

public List<Document> findCompletedTripsByDriver(String driverId) {
return trips.find(Filters.and(
Filters.eq(fieldName: "trip_status", value: "completed"),
Filters.eq(fieldName: "driver_id", driverId)
)).into(new ArrayList<>());
}

The following code example shows the converted query from the Java code above:

var driver_id = "<driver ID>"
db.getSiblingsDB("production")
.getCollection("trips")
.find({
"$and" : [
{ "trip_status" : "completed" },
{ "driver_id" : driver_id }
],
})

In this example, driver_id is a variable that holds a value determined at runtime. In order to test that your query outputs the results you expect, you must specify a test value by replacing <driver ID> with the driver ID. For example, driver_id = "1a2b3c4d5e".

Once you are satisfied with the query, you can run it in the Playground and view the query results.

Click Run to run your query and view the results.

Back

Missing Index Warning