| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
| * | ||
| * @remarks This option is ignored by the server. | ||
| * @see https://www.mongodb.com/docs/manual/reference/command/createIndexes/ | ||
| * @deprecated 4.2 |
There was a problem hiding this comment.
We removed 4.2 support recently, so we may not need this method at all. Can you verify?
Sorry, something went wrong.
There was a problem hiding this comment.
👀
Sorry, something went wrong.
There was a problem hiding this comment.
Based on chat offline, deprecated here means discouraged but not removed. So the option remains until the next major version of the server removes it. At that time, we can remove it too.
Sorry, something went wrong.
|
|
||
| const validIndexOptions = resolveIndexDescription( | ||
| userIndex, | ||
| // TODO(seanrmilligan): Add NODE ticket to set to remove allowUnknownIndexOptions with |
There was a problem hiding this comment.
Outstanding TODO.
Sorry, something went wrong.
| collectionName: string, | ||
| indexes: IndexDescription[], | ||
| options?: CreateIndexesOptions | ||
| indexOptions?: CreateIndexesOptions, |
There was a problem hiding this comment.
This parameter is not used in this function, think line 518 should be commandOptions ?? indexOptions.
Sorry, something went wrong.
There was a problem hiding this comment.
Done.
Sorry, something went wrong.
| collectionName: string, | ||
| indexes: IndexDescription[], | ||
| allowUnknownIndexOptions: boolean, | ||
| commandOptions?: CreateIndexesOptions | CreateIndexOptions |
There was a problem hiding this comment.
Optional params on a private constructor doesn't buy us anything and is detrimental, suggest making this mandatory. (Detrimental because "I forgot to pass a parameter" and "I don't have anything to specify" become indistinguishable.)
Sorry, something went wrong.
There was a problem hiding this comment.
Unresolved moderate issues affect overload typing, command-option typing, and serialization of language options.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overviewAdds opt-in passthrough for unknown index options while separating index and command options for createIndex.
Changes:
| File | Summary |
|---|---|
| test/unit/operations/indexes.test.ts | Tests option filtering and passthrough behavior. |
| test/unit/collection.test.ts | Tests command and index option separation. |
| test/integration/index-management/create_indexes_option_validation.test.ts | Adds integration validation coverage. |
| test/integration/index_management.test.ts | Tests unknown-option handling. |
| test/integration/crud/abstract_operation.test.ts | Updates operation construction tests. |
| src/utils.ts | Resolves inherited command options. |
| src/operations/indexes.ts | Defines option types, filtering, and passthrough logic. |
| src/operations/create_collection.ts | Updates internal index creation. |
| src/index.ts | Exports new public option types. |
| src/gridfs/upload.ts | Documents future option migration. |
| src/db.ts | Updates createIndex operation construction. |
| src/collection.ts | Adds overloads and passthrough support. |
src/collection.ts:739
// TODO(seanrmilligan): default this to true and remove the parameter in a future major
// release. Index options live on each index description, so nothing on this path
// contaminates them -- but flipping it turns today's silently dropped unknown option into
// a server error.
src/collection.ts:655
indexOptions?: IndexOptions,
commandOptions?: CreateIndexOptions
src/gridfs/upload.ts:278
// the index option allowlist. When validateOptions defaults to false, move the command
// options into the third parameter.
src/gridfs/upload.ts:387
// TODO(NODE-6893): timeoutMS is a command option; move it into the third parameter when
// validateOptions defaults to false.
src/operations/indexes.ts:479
comment?: Document;
src/operations/indexes.ts:479
/** * Enables users to specify an arbitrary comment to help trace the operation through * the database profiler, currentOp and logs. The default is to not send a value. * * @see https://www.mongodb.com/docs/manual/reference/command/createIndexes/ * * @sinceServerVersion 4.4 */ comment?: Document;
src/operations/indexes.ts:610
// TODO(seanrmilligan): Add NODE ticket to set to remove allowUnknownIndexOptions with
// a default behavior of true in a future 8.0.0 release
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Sorry, something went wrong.
| * | ||
| * This options is only supported by servers \>= 6.0. | ||
| */ | ||
| clustered?: boolean; |
There was a problem hiding this comment.
Good point. If clustered is only every present on the response*, why do we need to expose it on a request object.
*Side question: how/when do we return clustered to the user? I'm not finding it yet.
Sorry, something went wrong.
There was a problem hiding this comment.
Hmm, I adopted it wholesale from the spec's IndexOptions interface (clustered seen here: https://github.com/mongodb/specifications/blob/a9adc41b6eed59fd1197e852a2d8dccc4eaf16fb/source/index-management/index-management.md?plain=1#L806) but I'm not seeing IndexOptions itself used as the return type on any function, so perhaps it's a leftover?
We could bring that and the deprecated 4.2 field to the maintainers and see if it just needs a little cleanup.
Sorry, something went wrong.
There was a problem hiding this comment.
Good idea to bring this up with the maintainers. My thought is that we should remove clustered and background, but let's see.
For clustered, that information comes through on the Document in this signature:
export type IndexDescriptionInfo = Omit<IndexDescription, 'key' | 'version'> & {
key: { [key: string]: IndexDirection };
v?: IndexDescription['version'];
} & Document; // ← here
So then you can retrieve that data like this:
await db.createCollection('c', {
clusteredIndex: { key: { _id: 1 }, unique: true, name: 'my_clustered_id' }
});
await db.collection('c').listIndexes().toArray();
// [ { v: 2, key: { _id: 1 }, name: 'my_clustered_id', unique: true, clustered: true } ]
Reading clustered works now, so there's no reason to add it.
Sorry, something went wrong.
| indexSpecs: IndexDescription[], | ||
| options?: CreateIndexesOptions | ||
| commandOptions?: CreateIndexesOptions, | ||
| allowUnknownIndexOptions = false |
There was a problem hiding this comment.
When calling createIndex, a user opts-in to the new behavior by specifying an object.
This is not the same approach for createIndexes, where opt-in behavior is signaled with a boolean.
Should we have the same approaches in both spots, that is, specify an object to get the new behavior.
Sorry, something went wrong.
There was a problem hiding this comment.
With createIndex I was able to discriminate on parameter count (2 vs. 3) because the signature was changing. createIndexes on the other hand has the types it needs so I wasn't able to use a new signature vs. old signature to do the same discrimination
Sorry, something went wrong.
There was a problem hiding this comment.
Discussed offline, the plan is to document this behavior for the upcoming release, then remove it in v8.
Sorry, something went wrong.
There was a problem hiding this comment.
There are a number of type additions, but no new types tests. You'll need to add these.
Sorry, something went wrong.
| * | ||
| * This options is only supported by servers \>= 6.0. | ||
| */ | ||
| clustered?: boolean; |
There was a problem hiding this comment.
Good point. If clustered is only every present on the response*, why do we need to expose it on a request object.
*Side question: how/when do we return clustered to the user? I'm not finding it yet.
Sorry, something went wrong.
| const validProvidedOptions = Object.entries(description).filter(([optionName]) => | ||
| VALID_INDEX_OPTIONS.has(optionName) | ||
| const providedOptions = Object.entries(description).filter( | ||
| ([optionName]) => allowUnknownIndexOptions || VALID_INDEX_OPTIONS.has(optionName) |
There was a problem hiding this comment.
Think we need to test optionName !== 'key' here.
Sorry, something went wrong.
There was a problem hiding this comment.
Done. Filtered out key even when allowUnknownIndexOptions is true because I saw it is re-added by the caller to resolveIndexDescription
Sorry, something went wrong.
|
2 tests are failing across the variants (looks relevant to changes): [2026/09/11 16:19:54.080] 2 failing
[2026/09/11 16:19:54.080] 1) createIndex option validation
[2026/09/11 16:19:54.080] when command options are given (three parameter form)
[2026/09/11 16:19:54.080] creates an index using a server option the driver does not know about:
[2026/09/11 16:19:54.080] MongoServerError: Error in specification { prepareUnique: true, key: { e: 1 }, name: "e_1" } :: caused by :: The field 'prepareUnique' is not valid for an index specification. Specification: { prepareUnique: true, key: { e: 1 }, name: "e_1" }
[2026/09/11 16:19:54.080] at Connection.sendCommand (src/cmap/connection.ts:174:4)
[2026/09/11 16:19:54.080] at processTicksAndRejections (node:internal/process/task_queues:104:5)
[2026/09/11 16:19:54.080] at async Connection.command (src/cmap/connection.ts:174:4)
[2026/09/11 16:19:54.080] at async Server.command (src/sdam/server.ts:68:58)
[2026/09/11 16:19:54.080] at async executeOperationWithRetries (src/operations/execute_operation.ts:92:6)
[2026/09/11 16:19:54.080] at async executeOperation (src/operations/execute_operation.ts:24:2576)
[2026/09/11 16:19:54.080] at async Collection.createIndex (src/collection.ts:151:267)
[2026/09/11 16:19:54.080] at async Context.<anonymous> (test/integration/index-management/create_indexes_option_validation.test.ts:166:9)
[2026/09/11 16:19:54.080]
[2026/09/11 16:19:54.080] 2) createIndexes option validation
[2026/09/11 16:19:54.080] when command options are given
[2026/09/11 16:19:54.080] creates an index using a server option the driver does not know about:
[2026/09/11 16:19:54.080] MongoServerError: Error in specification { key: { e: 1 }, name: "e_1", prepareUnique: true } :: caused by :: The field 'prepareUnique' is not valid for an index specification. Specification: { key: { e: 1 }, name: "e_1", prepareUnique: true }
[2026/09/11 16:19:54.080] at Connection.sendCommand (src/cmap/connection.ts:174:4)
[2026/09/11 16:19:54.080] at processTicksAndRejections (node:internal/process/task_queues:104:5)
[2026/09/11 16:19:54.080] at async Connection.command (src/cmap/connection.ts:174:4)
[2026/09/11 16:19:54.080] at async Server.command (src/sdam/server.ts:68:58)
[2026/09/11 16:19:54.080] at async executeOperationWithRetries (src/operations/execute_operation.ts:92:6)
[2026/09/11 16:19:54.080] at async executeOperation (src/operations/execute_operation.ts:24:2576)
[2026/09/11 16:19:54.080] at async Collection.createIndexes (src/collection.ts:187:170)
[2026/09/11 16:19:54.080] at async Context.<anonymous> (test/integration/index-management/create_indexes_option_validation.test.ts:357:9)
|
Sorry, something went wrong.
I suspect this is because the wrong server version was used. Waiting on evergreen to confirm latest push. |
Sorry, something went wrong.
| } | ||
|
|
||
| // Maps to `IndexOptions` in | ||
| // https://github.com/mongodb/specifications/blob/6f64d0ee3ae49edbdb30eb995f3e29549e8cfa6a/source/index-management/index-management.md#common-api-components |
There was a problem hiding this comment.
This interface is missing finestIndexedLevel and coarsestIndexedLevel, which are called out in the AC.
And in a related question, do we have a DRIVERS ticket that updates the specifications repo with these additions as well? I'm having trouble finding these.
Sorry, something went wrong.
| // Maps to `IndexOptions` in | ||
| // https://github.com/mongodb/specifications/blob/6f64d0ee3ae49edbdb30eb995f3e29549e8cfa6a/source/index-management/index-management.md#common-api-components | ||
| /** @public */ | ||
| export interface IndexOptions { |
There was a problem hiding this comment.
This interface is closed, unknown options cannot be added to it. (That's why we need to keep adding @ts-expect-error in the tests, because there is currently no way to specify extra fields.)
I think we may need to add extend from Document here, to allow arbitrary string-key values:
export interface IndexOptions extends Document {
This should allow us to specify new fields without needing to suppress TS issues.
Sorry, something went wrong.
There was a problem hiding this comment.
This might be a feature? In this scenario where this happens, the driver is lagging in adding an option to the interface after the server adds support for it and that the user is on the bleeding edge by adding it themselves.
@ts-expect-error is an obvious hint that a property is being added that isn't declared in the interface. When the driver adds it, @ts-expect-error will itself start to error, sending the user a signal that they are no longer force-adding an unknown option.
That may or may not be a useful signal, in which case I can extend from Document.
Sorry, something went wrong.
There was a problem hiding this comment.
It seems strange to need a suppression for a "routine" use of the Driver, where "routine" means "I want to use an index feature my driver version isn't aware of". I don't think there's any benefit to "requiring" this hint, so let's extend from Document.
Sorry, something went wrong.
There was a problem hiding this comment.
Can you also add release notes to the PR description? We'll definitely need those.
Sorry, something went wrong.
| * | ||
| * @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 |
There was a problem hiding this comment.
Need to add @deprecated and a message here, something like:
* @deprecated Use the three parameter overload, which separates index options from * command options. This overload will be removed in a future major release.
Sorry, something went wrong.
There was a problem hiding this comment.
This looks like something that will eventually require compatibility work in mongosh, let's make sure to give this ticket the appropriate downstream changes marker
Sorry, something went wrong.
| * @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. Defaults to `false`; this will become the only | ||
| * behaviour in a future major release. |
There was a problem hiding this comment.
Should this also be deprecated then?
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Description
Summary of Changes
Differentiates between options for indexes and options for commands in the createIndex and createIndexes API.
More granular changes:
Notes for Reviewers
Types
There are many similar type names floating around. Some come by design from the spec, and some from the history of the API in this area. Note the differences between:
CreateIndexesOperation
The CreateIndexesOperation command object is created on both the createIndex and createIndexes paths. It accepts an array of indexes, where the createIndex path is the special case of an array with only one item. There is no corresponding CreateIndexOperation for creating a single index.
allowUnknownIndexOptions
allowUnknownIndexOptions is inferred transparently on behalf of the consumer of the createIndex API by detecting whether the caller called the two parameter overload (old behavior, set to false) or the three parameter overload (new, set to true). Using the createIndex(<3>) overload is considered as opting into the new passthrough behavior.
allowUnknownIndexOptions cannot be inferred on the createIndexes path because the types on createIndexes already align with the spec.
What is the motivation for this change?
This is in support of achieving "passthrough" behavior where options are validated by the server rather than the driver. Validation of options by the server rather than the driver accomplishes two goals:
Release Highlight
Release notes highlight
Double check the following