Query MongoDB with SQL

Which job runs took longest in each release? Use a common table expression and a window function against MongoDB. KeplerGrid shows the result rows and the aggregation it generates.

Rank the slowest runs in each release

Open Sample data → acme → job_runs → SQL. Choose Examples → Slowest runs per release, then Run SQL. The built-in workspace has 72 synthetic job runs. You can also paste this query into the SQL editor.

WITH ranked_runs AS (
  SELECT [release] AS release_name, _id AS run_id, service, duration_ms,
         RANK() OVER (PARTITION BY [release] ORDER BY duration_ms DESC) AS slow_rank
  FROM job_runs WHERE duration_ms IS NOT NULL AND [release] IS NOT NULL
)
SELECT release_name, slow_rank, service, duration_ms, run_id
FROM ranked_runs WHERE slow_rank <= 3
ORDER BY release_name DESC, slow_rank, run_id;

Download the SQL example

Read the nine results

Results from the original sample, ordered by release and rank
ReleaseRankServiceDuration (ms)
2026.12.31auth-worker8,120
2026.12.31auth-worker8,120
2026.12.33auth-worker5,876
2026.12.21auth-worker5,193
2026.12.22auth-worker4,825
2026.12.23auth-worker3,481
2026.12.11auth-worker4,510
2026.12.12auth-worker4,142
2026.12.13notifier3,446

For release 2026.12.3, two runs took 8,120 ms. Both rank first. The next took 5,876 ms and ranks third. Ranking starts again for each release. The final run_id sort makes the order of tied rows repeatable.

All runs tied at the cutoff qualify. For exactly three rows in a release with at least three eligible runs, replace RANK() with ROW_NUMBER() and add _id after duration_ms DESC in its window order.

KeplerGrid desktop, built-in sample on macOS · 15 seconds
Read the clip transcript
  1. Choose Slowest runs per release in the SQL examples for acme.job_runs.
  2. Generated MongoDB shows the window stage that assigns a rank within each release.
  3. Read the result. The two 8,120 ms runs tie for first. The 5,876 ms run is third.

Inspect the MongoDB pipeline

Choose Generated MongoDB. The app sends a read-only aggregation to the active database on a connected server. This is the window stage from the sample query. Earlier stages prepare the sort key and the later stages apply the rank cutoff and final ordering.

{
  "$setWindowFields": {
    "partitionBy": {
      "p0": {
        "$ifNull": [
          "$s0.release",
          null
        ]
      }
    },
    "sortBy": {
      "win0_k0": -1
    },
    "output": {
      "win0_rank": {
        "$rank": {}
      }
    }
  }
}

Download the complete generated pipeline

Use Explain on a live connection for MongoDB’s execution plan. The account must be able to read each referenced collection. Window functions require MongoDB 5.0 or later.

Keep the query and its results

Save the text with File → Save SQL…. Select a statement or place the cursor inside one before running a longer script. Table and JSON show the last successful run. On a live connection, File → Export full last run reruns that statement with its SQL limit and offset. A concurrent change can affect the new read.