Skip to content

Commit 4df3357

Browse files
authored
feat(data-modeling): allow partial documents match when discovering relationships on ObjectId fields (#7488)
1 parent 2d71caf commit 4df3357

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/compass-data-modeling/src/store/relationships.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,24 @@ export async function inferForeignToLocalRelationshipsForCollection(
180180
if (sampleDocs.length === 0) {
181181
return null;
182182
}
183+
// For high cardinality values like ObjectId we will consider
184+
// relationship existing even if a single matching documet was found
185+
// for any of the used sample values. Otherwise, to reduce the risk
186+
// of erroneously identifined relations, we will always expect an
187+
// exact match
188+
const expectedCount =
189+
idSchema.bsonType === 'objectId' ? 1 : sampleDocs.length;
183190
const matchingDocCount = await dataService.count(
184191
foreignNamespace,
185192
{
186193
_id: {
187194
$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
188195
},
189196
},
190-
{ hint: { _id: 1 }, maxTimeMS: 10_000 },
197+
{ hint: { _id: 1 }, maxTimeMS: 10_000, limit: expectedCount },
191198
{ abortSignal, fallbackReadPreference: 'secondaryPreferred' }
192199
);
193-
if (matchingDocCount !== sampleDocs.length) {
200+
if (matchingDocCount !== expectedCount) {
194201
return null;
195202
}
196203
return [

0 commit comments

Comments
 (0)