From 17320bdd42a2ff77d39fd0a2836a217718176b73 Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Wed, 22 Oct 2025 16:04:57 +0200 Subject: [PATCH] feat(data-modeling): allow partial documents match when discovering relationships on ObjectId fields --- .../compass-data-modeling/src/store/relationships.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/compass-data-modeling/src/store/relationships.ts b/packages/compass-data-modeling/src/store/relationships.ts index f8387052feb..b6567ea1498 100644 --- a/packages/compass-data-modeling/src/store/relationships.ts +++ b/packages/compass-data-modeling/src/store/relationships.ts @@ -180,6 +180,13 @@ export async function inferForeignToLocalRelationshipsForCollection( if (sampleDocs.length === 0) { return null; } + // For high cardinality values like ObjectId we will consider + // relationship existing even if a single matching documet was found + // for any of the used sample values. Otherwise, to reduce the risk + // of erroneously identifined relations, we will always expect an + // exact match + const expectedCount = + idSchema.bsonType === 'objectId' ? 1 : sampleDocs.length; const matchingDocCount = await dataService.count( foreignNamespace, { @@ -187,10 +194,10 @@ export async function inferForeignToLocalRelationshipsForCollection( $in: sampleDocs as any[], // driver wants this to be an ObjectId unless a generic type for the filter is provided, we don't currently support passing this generic value on data service level }, }, - { hint: { _id: 1 }, maxTimeMS: 10_000 }, + { hint: { _id: 1 }, maxTimeMS: 10_000, limit: expectedCount }, { abortSignal, fallbackReadPreference: 'secondaryPreferred' } ); - if (matchingDocCount !== sampleDocs.length) { + if (matchingDocCount !== expectedCount) { return null; } return [