**TL;DR:** The Query Targeting alert means queries scan orders of magnitude more documents than they return. Trace it in the Real-Time Performance Panel and index the filter. Query targeting Symptom: the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio. Sort by scanned.

## The error

```text
the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio. Sort by scanned.
```

## The fix

**TL;DR:** The Query Targeting alert means queries scan orders of magnitude more documents than they return. Trace it in the Real-Time Performance Panel and index the filter. Query targeting Symptom: the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio. Sort by scanned.

## The error

```text
the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio.
```

## The fix

# Query targeting

Symptom: the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio.

## Diagnose

Open the Real-Time Performance Panel during the slow period. It shows current operations with the documents-scanned vs documents-returned ratio per operation. Sort by scanned. The offenders are usually a `$match` on an unindexed field or a sort with no supporting index.

## Confirm

`explain("executionStats")` on the offending shape. Confirm COLLSCAN or an in-memory SORT, and note the exact filter and sort fields.

## Fix

Create a compound index covering the equality filters first, then the sort fields, then range filters (the ESR rule: equality, sort, range). One good compound index beats three single-field indexes.

## Verify

Re-run the workload: the scanned-to-returned ratio in the RTPP drops toward 1:1, and the Query Targeting alert clears. If the alert persists, another shape is still scanning; repeat.

## When to use this

- You are seeing this exact error message; match the block above, not just part of it.
- The failing call matches the scenario in the title: Atlas.
- You want the fastest verified fix before digging through logs.

## When not to use this

- Your error text differs from the block above; close cousins often have different causes.
- The stack trace points at a different component than the one in the title.
- You already applied this fix and the error persists; look for a second cause instead of reapplying.

## Compatibility

- Not pinned to a specific version; follows current Atlas behavior.