|
| 1 | +.. _intellij-run-java-queries: |
| 2 | + |
| 3 | +================ |
| 4 | +Run Java Queries |
| 5 | +================ |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Definition |
| 16 | +---------- |
| 17 | + |
| 18 | +The {+intellij-full+} enables you to run queries written in Java directly |
| 19 | +in the Database Explorer Playgrounds. |
| 20 | + |
| 21 | +The Run button appears next to your MongoDB queries. |
| 22 | + |
| 23 | +.. image:: /images/run-queries/intellij-run-icon.png |
| 24 | + :alt: Click the Run button to convert your Java query to mongosh syntax. |
| 25 | + |
| 26 | +Behavior |
| 27 | +-------- |
| 28 | + |
| 29 | +When you click the Run button, the plugin automatically converts your |
| 30 | +Java query to ``mongosh`` syntax and opens a Playground file with the |
| 31 | +populated query. |
| 32 | + |
| 33 | +For field values that are variables determined at runtime, the plugin |
| 34 | +creates a placeholder variable. You can populate this placeholder with |
| 35 | +a test value and run the query in the Playground. |
| 36 | + |
| 37 | +Example |
| 38 | +------- |
| 39 | + |
| 40 | +In this example, the Java query on the ``production.trips`` collection |
| 41 | +resemles the following: |
| 42 | + |
| 43 | +.. code:: java |
| 44 | + :copyable: false |
| 45 | + |
| 46 | + public List<Document> findCompletedTripsByDriver(String driverId) { |
| 47 | + return trips.find(Filters.and( |
| 48 | + Filters.eq(fieldName: "trip_status", value: "completed"), |
| 49 | + Filters.eq(fieldName: "driver_id", driverId) |
| 50 | + )).into(new ArrayList<>()); |
| 51 | + } |
| 52 | + |
| 53 | +The code example below shows the converted query from the Java code |
| 54 | +above. |
| 55 | + |
| 56 | +.. code:: javascript |
| 57 | + :copyable: true |
| 58 | + |
| 59 | + var driver_id = "<driver ID>" |
| 60 | + |
| 61 | + db.getSiblingsDB("production") |
| 62 | + .getCollection("trips") |
| 63 | + .find( { |
| 64 | + "$and" : [ |
| 65 | + { "trip_status" : "completed" }, |
| 66 | + { "driver_id" : driver_id } |
| 67 | + ] |
| 68 | + ) |
| 69 | + |
| 70 | +In this example, ``driver_id`` is a variable that holds a value |
| 71 | +determined at runtime. In order to test that your query outputs the |
| 72 | +results you expect, you must specify a test value by replacing |
| 73 | +``<driver ID>`` with the driver ID. For example, |
| 74 | +``driver_id = "1a2b3c4d5e"``. |
| 75 | + |
| 76 | +Once you are satisfied with the query, you can run it in the Playground |
| 77 | +and view the query results. |
| 78 | + |
| 79 | +.. image:: /images/run-queries/intellij-pg-icon-converted-query.png |
| 80 | + :alt: Click the Run button to run your query and view the results. |
0 commit comments