Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/compass-data-modeling/src/store/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,24 @@ 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,
{
_id: {
$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 [
Expand Down
Loading