Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8a0866f
Allow passthrough options on createIndexes
Jul 28, 2026
991884f
Fix: createIndexes / fromIndexDescriptionArray interaction
Sep 11, 2026
ab02deb
drop ticket number from test db string name
Sep 14, 2026
e1ad022
Inherit CSOT and other fields from CommandOperationOptions into Creat…
Sep 14, 2026
4a4310c
support public api spec / wire property naming differences with a map
Sep 15, 2026
214cd1a
prepareUnique requires 6.0+
Sep 15, 2026
4c1630b
remove metadata
Sep 15, 2026
4d84948
Remove 'key' in resolveIndexDescription, even when allowUnknownIndexO…
Sep 17, 2026
44b38b1
Update TODO with ticket number
Sep 23, 2026
44c62ee
remove optionality from CreateIndexesOperation options/commandOptions…
Sep 23, 2026
f560c71
deprecate option for removal in 8.x
Sep 23, 2026
e0ec424
Extend Document to allow unknown options without a @ts-expect-error o…
Sep 24, 2026
30f40b0
Add 2dsphere options to IndexOptions interface
Sep 24, 2026
56d06d9
remove ts-expect-error
Sep 24, 2026
2712b41
Add deprecated comment to two-parameter overload of createIndex
Sep 24, 2026
b3279ed
change how I deprecated the allowUnknownIndexOptions parameter on cre…
Sep 24, 2026
7339d8a
type tests
Sep 24, 2026
9cdbeef
Merge remote-tracking branch 'origin/main' into sean.milligan/createI…
Sep 24, 2026
293bdc2
more TODO -> ticket number updates
Sep 25, 2026
585c7fd
refactor(NODE-6893): require explicit command options on CreateIndexe…
Sep 30, 2026
f9fac6f
Remove clustered, finestIndexedLevel, coarsestIndexedLevel
Sep 30, 2026
04ea05d
refactor(NODE-6893): require both arguments on the three parameter cr…
Sep 30, 2026
8a5596f
refactor(NODE-6893): remove background from IndexOptions
Sep 30, 2026
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
93 changes: 79 additions & 14 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ import {
import {
CreateIndexesOperation,
type CreateIndexesOptions,
type CreateIndexOptions,
type DropIndexesOptions,
DropIndexOperation,
type IndexDescription,
type IndexDescriptionCompact,
type IndexDescriptionInfo,
type IndexInformationOptions,
type IndexOptions,
type IndexSpecification,
type ListIndexesOptions
} from './operations/indexes';
Expand Down Expand Up @@ -609,7 +611,7 @@ export class Collection<TSchema extends Document = Document> {
* Creates an index on the db and collection collection.
*
* @param indexSpec - The field name or index specification to create an index for
* @param options - Optional settings for the command
* @param indexOptions - Optional settings for the command
Comment thread
PavelSafronov marked this conversation as resolved.
*
* @example
* ```ts
Expand All @@ -632,20 +634,55 @@ export class Collection<TSchema extends Document = Document> {
* // Equivalent to { j: 1, k: -1, l: 2d }
* await collection.createIndex(['j', ['k', -1], { l: '2d' }])
* ```
* @deprecated Use the three parameter overload, which separates index options from
* command options. This overload will be removed in a future major release.
*/
createIndex(indexSpec: IndexSpecification, options?: CreateIndexesOptions): Promise<string>;

/**
* Creates an index on the db and collection collection.
*
* Options for the index itself are given in `indexOptions`, and options for the
* `createIndexes` command are given separately in `commandOptions`. Unknown fields in
* `indexOptions` are passed through to the server for validation rather than being dropped.
*
* @param keys - The field name or index specification to create an index for
* @param indexOptions - Settings for the index, or `undefined` if there are none
* @param commandOptions - Settings for the `createIndexes` command, or `undefined` if there are none
*/
createIndex(
Comment thread
PavelSafronov marked this conversation as resolved.
keys: IndexSpecification,
indexOptions: IndexOptions | undefined,
commandOptions: CreateIndexOptions | undefined
): Promise<string>;

async createIndex(
indexSpec: IndexSpecification,
options?: CreateIndexesOptions
indexOptions?: CreateIndexesOptions | IndexOptions,
commandOptions?: CreateIndexOptions
): Promise<string> {
const indexes = await executeOperation(
this.client,
CreateIndexesOperation.fromIndexSpecification(
this,
this.collectionName,
indexSpec,
resolveOptions(this, options)
)
);
// When commandOptions is provided the caller has separated the two kinds of options, so the
// index options are forwarded as-is and only the command options inherit from the parent.
// Otherwise indexOptions is both, and it inherits from the parent as it always has.
const operation =
commandOptions == null
? CreateIndexesOperation.fromIndexSpecification(
this,
this.collectionName,
indexSpec,
/*allowUnknownIndexOptions=*/ false,
resolveOptions(this, indexOptions), // at this point indexOptions is the combined index and command options
undefined
)
: CreateIndexesOperation.fromIndexSpecification(
this,
this.collectionName,
indexSpec,
/*allowUnknownIndexOptions=*/ true,
indexOptions,
resolveOptions(this, commandOptions)
);
const indexes = await executeOperation(this.client, operation);

return indexes[0];
}
Expand All @@ -659,7 +696,7 @@ export class Collection<TSchema extends Document = Document> {
* Index specifications are defined {@link https://www.mongodb.com/docs/manual/reference/command/createIndexes/| here}.
*
* @param indexSpecs - An array of index specifications to be created
* @param options - Optional settings for the command
* @param commandOptions - Optional settings for the `createIndexes` command
*
* @example
* ```ts
Expand All @@ -681,17 +718,45 @@ export class Collection<TSchema extends Document = Document> {
* ]);
* ```
*/
createIndexes(
indexSpecs: IndexDescription[],
commandOptions?: CreateIndexesOptions
): Promise<string[]>;

/**
* Creates multiple indexes in the collection, opting into "pass through" behavior for index
* options the driver does not recognise.
*
* @param indexSpecs - An array of index specifications to be created
* @param commandOptions - Optional settings for the `createIndexes` command
* @param allowUnknownIndexOptions - When `true`, index options the driver does not recognise are
* sent to the server instead of being dropped, for the server to validate.
*
* @deprecated Used to opt into "pass through" behavior, where options will be validated by the
* server rather than the driver. In a future release this overload will be removed and the
* default behavior will change from `false` to `true`.
*/
createIndexes(
indexSpecs: IndexDescription[],
commandOptions: CreateIndexesOptions | undefined,
allowUnknownIndexOptions: boolean
): Promise<string[]>;

async createIndexes(
indexSpecs: IndexDescription[],
options?: CreateIndexesOptions
commandOptions?: CreateIndexesOptions,
// TODO(NODE-7868): Remove allowUnknownIndexOptions with a default behavior of true in a future major version release
allowUnknownIndexOptions = false
Comment thread
PavelSafronov marked this conversation as resolved.
): Promise<string[]> {
return await executeOperation(
this.client,
CreateIndexesOperation.fromIndexDescriptionArray(
this,
this.collectionName,
indexSpecs,
resolveOptions(this, { ...options, maxTimeMS: undefined })
// TODO(NODE-7868): Remove allowUnknownIndexOptions with a default behavior of true in a future major version release
allowUnknownIndexOptions,
resolveOptions(this, { ...commandOptions, maxTimeMS: undefined })
)
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,14 @@ export class Db {
): Promise<string> {
const indexes = await executeOperation(
this.client,
CreateIndexesOperation.fromIndexSpecification(this, name, indexSpec, options)
CreateIndexesOperation.fromIndexSpecification(
this,
name,
indexSpec,
/*allowUnknownIndexOptions=*/ false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Can we omit /allowUnknownIndexOptions=/ everywhere? Checking the function definition is probably enough to make it clear what's being provided, and this doesn't seem to be a standard practice across the codebase.

options ?? {},
undefined
)
);
return indexes[0];
}
Expand Down
9 changes: 2 additions & 7 deletions src/gridfs/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,7 @@ async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise<void>
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(
`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
);
await stream.chunks.createIndex(index, {
...stream.writeConcern,
background: true,
unique: true,
timeoutMS: remainingTimeMS
});
await stream.chunks.createIndex(index, { unique: true }, { timeoutMS: remainingTimeMS });
}
}

Expand Down Expand Up @@ -379,7 +374,7 @@ async function checkIndexes(stream: GridFSBucketWriteStream): Promise<void> {
`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`
);

await stream.files.createIndex(index, { background: false, timeoutMS: remainingTimeMS });
await stream.files.createIndex(index, {}, { timeoutMS: remainingTimeMS });
}

await checkChunksIndex(stream);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,13 @@ export type {
export type { IndexInformationOptions } from './operations/indexes';
export type {
CreateIndexesOptions,
CreateIndexOptions,
DropIndexesOptions,
IndexDescription,
IndexDescriptionCompact,
IndexDescriptionInfo,
IndexDirection,
IndexOptions,
IndexSpecification,
ListIndexesOptions
} from './operations/indexes';
Expand Down
4 changes: 3 additions & 1 deletion src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ export async function createCollections<TSchema extends Document>(
db,
name,
{ __safeContent__: 1 },
{ session: options.session }
/*allowUnknownIndexOptions=*/ false,
{ session: options.session },
undefined
);
await executeOperation(db.client, createIndexOp, timeoutContext);
}
Expand Down
Loading
Loading