Skip to content

driver-mongodb indexes lookup joins off reference_to — a key the spec REFUSES — so no authored lookup field has ever been indexed on MongoDB #13222

Description

@os-zhuang

Measured while implementing #12252, which turned out to be masking this. Filed unassigned by the domain:engine PM seat; the disposition is a maintainer call (see the ⚠️ on #12251 below).

The defect

packages/drivers/driver-mongodb/src/mongodb-schema.ts creates a join index for lookup fields, gated on reference_to:

// :112-113
if (
  (field.type === 'lookup' && field.reference_to) ||
  field.type === 'user'
) {
  indexOps.push({ spec: { [fieldName]: 1 }, options: { name: `idx_${fieldName}_lookup` } });
}

and its local FieldDef declares that key and no other relationship spelling:

// :37
reference_to?: string;

Measured on origin/main: reference — the canonical and only spelling FieldSchema accepts — appears nowhere in this file as a read. The three other occurrences are reference_to itself, a prose comment, and the word "reference" inside an English sentence.

reference_to is a rejected alias. Per #12252's measurement, FieldSchema.safeParse({ type: 'lookup', reference_to: 'p' }) fails with unrecognized_keys. So an authored object cannot carry the key this branch tests for.

The lookup half of that condition is dead for every authored object. No lookup field has ever produced idx_<field>_lookup on MongoDB.

Why it looked healthy

Two things hid it, and both are measurable:

  1. The user disjunct is unconditional. field.type === 'user' needs no reference_to, so idx_owner_lookup-shaped indexes are created. Any spot-check that happened to look at a user field saw the feature working.

  2. One test fixture supplied the rejected key. packages/drivers/driver-mongodb/src/mongodb-driver.test.ts:347 is the only live trigger of the lookup arm anywhere in the tree, and its suite asserts the index exists (:366). Differential probe against the real syncCollectionSchema, through the same fake-Db recorder the package's own mongodb-schema-declared-indexes.test.ts uses:

    fixture spelling idx_company_id_lookup
    reference_to (current) present
    reference (canonical) absent
    positive control — a user field idx_owner_lookup present

    A minus B = ['idx_company_id_lookup']; B minus A = []. The control proves B's zero is a real zero and not a dead harness.

    ⚠️ And that suite is one of the five skipped on every ordinary CI lane (OS_TEST_MONGODB_MEMORY_SERVER_ENABLED=1, driver-mongodb 测试:两个套件并发下载 MongoDB 二进制,mongodb-memory-server 的 rename 竞态让全绿测试 exit 1(队列 flaky) #5517) — so the one test standing on this branch does not run.

Scale

Per #12251's own measurement: all 57 relationship fields across the 44 exported platform objects carry reference. So the count of authored lookup fields indexed on MongoDB today is zero, platform-wide.

⚠️ Relationship to #12251, stated plainly

#12251 covered this and was closed not_planned. This card is not a unilateral reversal of that — filing evidence is not overturning a ruling, and I am explicitly not reopening it. What is new since it was closed:

If the answer is still "not planned", that is a legitimate outcome — but it should be recorded against this evidence rather than inherited, because #12252's inverted pin will assert the absence as current behaviour and should name the card that owns it.

Deliverable, if ruled fixable

Read the canonical spelling in the branch and the FieldDef (accepting reference_to too, if back-compat with stored non-conforming data matters). ⚠️ This is a behaviour change on boot for existing deployments — index builds on large collections — which is exactly why #12251 asked for a deliberate decision. It should be graded on its own merits, not inherited from #12252's priority:p2 tidy-up.

Related

#12252 (the fixture card that surfaced it) · #12251 (closed not_planned) · #11567 (the SQL-side FK counterpart) · #5517 (why the covering suite is opt-in)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions