Skip to content

Commit 0ebc1ac

Browse files
fix(NODE-6019): indexExists always returns false when full is set to true (#4034)
1 parent 024e487 commit 0ebc1ac

15 files changed

+900
-1756
lines changed

src/bulk/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from '../error';
1313
import type { Filter, OneOrMore, OptionalId, UpdateFilter, WithoutId } from '../mongo_types';
1414
import type { CollationOptions, CommandOperationOptions } from '../operations/command';
15-
import { maybeAddIdToDocuments } from '../operations/common_functions';
1615
import { DeleteOperation, type DeleteStatement, makeDeleteStatement } from '../operations/delete';
1716
import { executeOperation } from '../operations/execute_operation';
1817
import { InsertOperation } from '../operations/insert';
@@ -21,6 +20,7 @@ import { makeUpdateStatement, UpdateOperation, type UpdateStatement } from '../o
2120
import type { Server } from '../sdam/server';
2221
import type { Topology } from '../sdam/topology';
2322
import type { ClientSession } from '../sessions';
23+
import { maybeAddIdToDocuments } from '../utils';
2424
import {
2525
applyRetryableWrites,
2626
type Callback,

src/collection.ts

+37-33
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
} from './mongo_types';
2525
import type { AggregateOptions } from './operations/aggregate';
2626
import { BulkWriteOperation } from './operations/bulk_write';
27-
import type { IndexInformationOptions } from './operations/common_functions';
2827
import { CountOperation, type CountOptions } from './operations/count';
2928
import { CountDocumentsOperation, type CountDocumentsOptions } from './operations/count_documents';
3029
import {
@@ -49,19 +48,16 @@ import {
4948
FindOneAndUpdateOperation,
5049
type FindOneAndUpdateOptions
5150
} from './operations/find_and_modify';
52-
import {
53-
CreateIndexesOperation,
54-
type CreateIndexesOptions,
55-
CreateIndexOperation,
56-
type DropIndexesOptions,
57-
DropIndexOperation,
58-
type IndexDescription,
59-
IndexesOperation,
60-
IndexExistsOperation,
61-
IndexInformationOperation,
62-
type IndexSpecification,
63-
type ListIndexesOptions
51+
import type {
52+
CreateIndexesOptions,
53+
DropIndexesOptions,
54+
IndexDescription,
55+
IndexDirection,
56+
IndexInformationOptions,
57+
IndexSpecification,
58+
ListIndexesOptions
6459
} from './operations/indexes';
60+
import { CreateIndexesOperation, DropIndexOperation } from './operations/indexes';
6561
import {
6662
InsertManyOperation,
6763
type InsertManyResult,
@@ -575,15 +571,17 @@ export class Collection<TSchema extends Document = Document> {
575571
indexSpec: IndexSpecification,
576572
options?: CreateIndexesOptions
577573
): Promise<string> {
578-
return executeOperation(
574+
const indexes = await executeOperation(
579575
this.client,
580-
new CreateIndexOperation(
581-
this as TODO_NODE_3286,
576+
CreateIndexesOperation.fromIndexSpecification(
577+
this,
582578
this.collectionName,
583579
indexSpec,
584580
resolveOptions(this, options)
585581
)
586582
);
583+
584+
return indexes[0];
587585
}
588586

589587
/**
@@ -623,8 +621,8 @@ export class Collection<TSchema extends Document = Document> {
623621
): Promise<string[]> {
624622
return executeOperation(
625623
this.client,
626-
new CreateIndexesOperation(
627-
this as TODO_NODE_3286,
624+
CreateIndexesOperation.fromIndexDescriptionArray(
625+
this,
628626
this.collectionName,
629627
indexSpecs,
630628
resolveOptions(this, { ...options, maxTimeMS: undefined })
@@ -680,14 +678,14 @@ export class Collection<TSchema extends Document = Document> {
680678
* @param indexes - One or more index names to check.
681679
* @param options - Optional settings for the command
682680
*/
683-
async indexExists(
684-
indexes: string | string[],
685-
options?: IndexInformationOptions
686-
): Promise<boolean> {
687-
return executeOperation(
688-
this.client,
689-
new IndexExistsOperation(this as TODO_NODE_3286, indexes, resolveOptions(this, options))
681+
async indexExists(indexes: string | string[], options?: ListIndexesOptions): Promise<boolean> {
682+
const indexNames: string[] = Array.isArray(indexes) ? indexes : [indexes];
683+
const allIndexes: Set<string> = new Set(
684+
await this.listIndexes(options)
685+
.map(({ name }) => name)
686+
.toArray()
690687
);
688+
return indexNames.every(name => allIndexes.has(name));
691689
}
692690

693691
/**
@@ -696,10 +694,7 @@ export class Collection<TSchema extends Document = Document> {
696694
* @param options - Optional settings for the command
697695
*/
698696
async indexInformation(options?: IndexInformationOptions): Promise<Document> {
699-
return executeOperation(
700-
this.client,
701-
new IndexInformationOperation(this.s.db, this.collectionName, resolveOptions(this, options))
702-
);
697+
return this.indexes({ ...options, full: options?.full ?? false });
703698
}
704699

705700
/**
@@ -804,10 +799,19 @@ export class Collection<TSchema extends Document = Document> {
804799
* @param options - Optional settings for the command
805800
*/
806801
async indexes(options?: IndexInformationOptions): Promise<Document[]> {
807-
return executeOperation(
808-
this.client,
809-
new IndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options))
810-
);
802+
const indexes = await this.listIndexes(options).toArray();
803+
const full = options?.full ?? true;
804+
if (full) {
805+
return indexes;
806+
}
807+
808+
const object: Record<
809+
string,
810+
Array<[name: string, direction: IndexDirection]>
811+
> = Object.fromEntries(indexes.map(({ name, key }) => [name, Object.entries(key)]));
812+
813+
// @ts-expect-error TODO(NODE-6029): fix return type of `indexes()` and `indexInformation()`
814+
return object;
811815
}
812816

813817
/**

src/db.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type { MongoClient, PkFactory } from './mongo_client';
1111
import type { TODO_NODE_3286 } from './mongo_types';
1212
import type { AggregateOptions } from './operations/aggregate';
1313
import { CollectionsOperation } from './operations/collections';
14-
import type { IndexInformationOptions } from './operations/common_functions';
1514
import {
1615
CreateCollectionOperation,
1716
type CreateCollectionOptions
@@ -24,9 +23,9 @@ import {
2423
} from './operations/drop';
2524
import { executeOperation } from './operations/execute_operation';
2625
import {
26+
CreateIndexesOperation,
2727
type CreateIndexesOptions,
28-
CreateIndexOperation,
29-
IndexInformationOperation,
28+
type IndexInformationOptions,
3029
type IndexSpecification
3130
} from './operations/indexes';
3231
import type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
@@ -426,10 +425,11 @@ export class Db {
426425
indexSpec: IndexSpecification,
427426
options?: CreateIndexesOptions
428427
): Promise<string> {
429-
return executeOperation(
428+
const indexes = await executeOperation(
430429
this.client,
431-
new CreateIndexOperation(this, name, indexSpec, resolveOptions(this, options))
430+
CreateIndexesOperation.fromIndexSpecification(this, name, indexSpec, options)
432431
);
432+
return indexes[0];
433433
}
434434

435435
/**
@@ -480,10 +480,7 @@ export class Db {
480480
* @param options - Optional settings for the command
481481
*/
482482
async indexInformation(name: string, options?: IndexInformationOptions): Promise<Document> {
483-
return executeOperation(
484-
this.client,
485-
new IndexInformationOperation(this, name, resolveOptions(this, options))
486-
);
483+
return this.collection(name).indexInformation(resolveOptions(this, options));
487484
}
488485

489486
/**

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ export type {
447447
CommandOperationOptions,
448448
OperationParent
449449
} from './operations/command';
450-
export type { IndexInformationOptions } from './operations/common_functions';
451450
export type { CountOptions } from './operations/count';
452451
export type { CountDocumentsOptions } from './operations/count_documents';
453452
export type {
@@ -466,6 +465,7 @@ export type {
466465
FindOneAndReplaceOptions,
467466
FindOneAndUpdateOptions
468467
} from './operations/find_and_modify';
468+
export type { IndexInformationOptions } from './operations/indexes';
469469
export type {
470470
CreateIndexesOptions,
471471
DropIndexesOptions,

src/operations/common_functions.ts

-79
This file was deleted.

src/operations/create_collection.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { PkFactory } from '../mongo_client';
1010
import type { Server } from '../sdam/server';
1111
import type { ClientSession } from '../sessions';
1212
import { CommandOperation, type CommandOperationOptions } from './command';
13-
import { CreateIndexOperation } from './indexes';
13+
import { CreateIndexesOperation } from './indexes';
1414
import { Aspect, defineAspects } from './operation';
1515

1616
const ILLEGAL_COMMAND_FIELDS = new Set([
@@ -167,7 +167,12 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
167167

168168
if (encryptedFields) {
169169
// Create the required index for queryable encryption support.
170-
const createIndexOp = new CreateIndexOperation(db, name, { __safeContent__: 1 }, {});
170+
const createIndexOp = CreateIndexesOperation.fromIndexSpecification(
171+
db,
172+
name,
173+
{ __safeContent__: 1 },
174+
{}
175+
);
171176
await createIndexOp.execute(server, session);
172177
}
173178

0 commit comments

Comments
 (0)